1. 程式人生 > >Spring boot 集成ActiveMQ(包含雙向隊列實現)

Spring boot 集成ActiveMQ(包含雙向隊列實現)

pen 處理器 tap location load sys xmlns 1.8 mem

  集百家之長,成一家之言。

1、 下載ActiveMQ

https://mirrors.tuna.tsinghua.edu.cn/apache//activemq/5.15.9/apache-activemq-5.15.9-bin.zip

2、新建 Maven 項目 activemq

3、pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
        http://maven.apache.org/xsd/maven-4.0.0.xsd"
> <modelVersion>4.0.0</modelVersion> <groupId>com.java</groupId> <artifactId>activemq</artifactId> <version>1.0.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId
>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> </parent> <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <
artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <!-- 熱部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.8.RELEASE</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>

4、ActiveMQStarter.java

package com.java;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 主啟動類
 * 
 * @author Logan
 * @version 1.0.0
 * @createDate 2019-05-08
 *
 */
@SpringBootApplication
public class ActiveMQStarter {

    public static void main(String[] args) {
        SpringApplication.run(ActiveMQStarter.class, args);
    }

}

5、SendMessageController.java

package com.java.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 發送消息類
 * 
 * @author Logan
 * @version 1.0.0
 * @createDate 2019-05-08
 *
 */
@RestController
public class SendMessageController {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    /**
     * 發送到的隊列名
     */
    private String destinationName = "handle-queue";

    @GetMapping("/send")
    public String send(String params) {
        System.out.println("[ 收到請求 ]");

        jmsMessagingTemplate.convertAndSend(destinationName, params);

        System.out.println("[ 返回響應 ]");
        return "您的任務已提交";
    }

    @JmsListener(destination = "result-queue") // 監聽處理結果隊列
    public void handle(String message) {
        System.out.println("[ 收到消息處理結果 ]" + System.currentTimeMillis());

        System.out.println(message);
    }

}

6、MessageHandler.java

package com.java.listener;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;

/**
 * 任務處理器,監聽ActiveMQ隊列中的消息,消費並處理
 * 
 * @author Logan
 * @version 1.0.0
 * @createDate 2019-05-08
 *
 */
@Component
public class MessageHandler {

    @SendTo("result-queue") // 處理結果發送到 "result-queue" 隊列中
    @JmsListener(destination = "handle-queue") // 監聽處理消息隊列
    public String handle(String message) {
        System.out.println("[ 處理器開始處理消息 ]" + System.currentTimeMillis());

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(message);

        System.out.println("[ 處理器處理消息完成 ]" + System.currentTimeMillis());

        return "消息“" + message + "”處理完成";
    }

}

7、application.properties

spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.in-memory=true  
spring.activemq.pool.enabled=false

8、啟動

運行 activemq.bat 啟動ActiveMQ 服務

運行ActiveMQStarter.java 啟動項目

9、測試

瀏覽器輸入 http://127.0.0.1:8080/send?params=Hello

瀏覽器快速收到響應 “您的任務已提交”,控制臺日誌如下:

[ 收到請求 ]
[ 處理器開始處理消息 ]1557311835328
[ 返回響應 ]
Hello
[ 處理器處理消息完成 ]1557311840353
[ 收到消息處理結果 ]1557311840369
消息“Hello”處理完成

雙向通信正常!

查看消息隊列服務

瀏覽器輸入 http://127.0.0.1:8161

登錄 admin/admin

可查看所有隊列以及生產者和消費者情況

截圖如下:

技術分享圖片

Spring boot 集成ActiveMQ(包含雙向隊列實現)

.

Spring boot 集成ActiveMQ(包含雙向隊列實現)