• 转 Spring boot 集成ActiveMQ(包含双向队列实现)


    https://www.cnblogs.com/jonban/p/activemq.html

    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(包含双向队列实现)

  • 相关阅读:
    C#微信公众号开发 -- (一)开发之前的准备
    C#微信公众号学习
    微信模板消息发送帮助类
    Java平台调用.net开发的WebService报错处理
    sql语句单据编号生成防并发
    [转]实用教程:搭建FTP服务器以实现局域网飞速传输文件
    C# Post Json数据到对方url
    sql语句优化技巧
    Html添加百度地图
    查看CentOS版本
  • 原文地址:https://www.cnblogs.com/telwanggs/p/12620549.html
Copyright © 2020-2023  润新知