Spring Boot 快速上手(七)集成ActiveMQ

来源:互联网 发布:python时钟组件 编辑:程序博客网 时间:2024/05/16 10:47

1.ActiveMQ下载和启动

①下载ActiveMQ

ActiveMQ的官方下载地址是http://activemq.apache.org/download.html,浏览器访问该地址,页面如下:


在上面的页面中,点击红色框中的链接地址可进入最新发行版的下载页,当前最新发行版是5.15.2;点击蓝色框中的链接地址可进入历史版本的下载页。这里选择下载最新发行版的Windows版本,下载后得到文件apache-activemq-5.15.2-bin.zip。

②启动ActiveMQ服务

解压ActiveMQ的压缩包文件,解压后的文件夹结构如下:

进入bin目录,然后根据当前Windows系统选择进入win64目录或是win32目录,可看到如下目录结构:

在上面的文件中,InstallService.bat用来在系统服务中添加ActiveMQ服务,activemq.bat用来启动ActiveMQ服务。
单击activemq.bat启动服务,访问http://localhost:8161/可以打开ActiveMQ的管理后台,默认登录名是admin,默认登录密码是admin。

2.集成ActiveMQ

①新建项目

为了更准确的测试消息的发送和接收,这里把发送和接收放在两个项目里,同时为了方便配置管理,在两个子项目之上维护一个父项目,项目具体结构如下:

其中,父项目boot-activemq的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>net.xxpsw.demo</groupId><artifactId>boot-activemq</artifactId><version>0.0.1-SNAPSHOT</version><packaging>pom</packaging><name>Spring Boot ActiveMQ</name><description>Spring Boot ActiveMQ</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.7.RELEASE</version></parent><modules><module>boot-activemq-client</module><module>boot-activemq-server</module></modules><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId></dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-client</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
子项目boot-activemq-client的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><artifactId>boot-activemq-client</artifactId><packaging>jar</packaging><name>Spring Boot ActiveMQ Client</name><description>Spring Boot ActiveMQ Client</description><parent><groupId>net.xxpsw.demo</groupId><artifactId>boot-activemq</artifactId><version>0.0.1-SNAPSHOT</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
子项目boot-activemq-client的application.properties文件内容如下:
# 访问端口server.port=8080# 指定日志文件位置logging.file=E:/spring-boot-activemq-client.log# 指定日志输出级别logging.level.org.springframework.web=ERRORlogging.level.org.apache=ERRORlogging.level.org.springframework.boot.web=ERROR
子项目boot-activemq-server的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><artifactId>boot-activemq-server</artifactId><packaging>jar</packaging><name>Spring Boot ActiveMQ Server</name><description>Spring Boot ActiveMQ Server</description><parent><groupId>net.xxpsw.demo</groupId><artifactId>boot-activemq</artifactId><version>0.0.1-SNAPSHOT</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
子项目boot-activemq-server的application.properties文件内容如下:
# 访问端口server.port=8090# 指定日志文件位置logging.file=E:/spring-boot-activemq-server.log# 指定日志输出级别logging.level.org.springframework.web=ERRORlogging.level.org.apache=ERRORlogging.level.org.springframework.boot.web=ERROR

②ActiveMQ的配置参数

SpringBoot中ActiveMQ的配置类包路径是org.springframework.boot.autoconfigure.jms.activemq.ActiveMQProperties,该类基本结构如下:
@ConfigurationProperties(prefix = "spring.activemq")public class ActiveMQProperties {// URL of the ActiveMQ broker. Auto-generated by default.private String brokerUrl;// Specify if the default broker URL should be in memory. Ignored if an explicit broker has been specified.private boolean inMemory = true;// Login user of the broker.private String user;// Login password of the broker.private String password;// Time to wait, in milliseconds, before considering a close complete.private int closeTimeout = 15000;// Time to wait, in milliseconds, on Message sends for a response. Set it to 0 to indicate to wait forever.private int sendTimeout = 0;private boolean nonBlockingRedelivery = false;private Pool pool = new Pool();private Packages packages = new Packages();}
由源码可知,ActiveMQ配置项的前缀是spring.activemq,在boot-activemq-client和boot-activemq-server的配置文件中分别追加如下配置:
# 消息代理路径spring.activemq.broker-url=tcp://localhost:61616

③ActiveMQ消息发送端

在boot-activemq-server项目中新建消息类Msg:
package net.xxpsw.demo.springboot.activemq.server.send;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.Session;import org.springframework.jms.core.MessageCreator;public class Msg implements MessageCreator {/** 消息内容 */private String message;public Msg(String message) {super();this.message = message;}@Overridepublic Message createMessage(Session session) throws JMSException {return session.createTextMessage(message);}}
新建发送消息接口Sender及其实现类SenderImpl:
package net.xxpsw.demo.springboot.activemq.server.send;public interface Sender {public void send(String destination, String message) throws Exception;}
package net.xxpsw.demo.springboot.activemq.server.send;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.CommandLineRunner;import org.springframework.jms.core.JmsTemplate;import org.springframework.stereotype.Component;@Componentpublic class SenderImpl implements Sender, CommandLineRunner {private String message;private String destination;@Autowiredprivate JmsTemplate jmsTemplate;@Overridepublic void run(String... args) throws Exception {if (null != destination) {jmsTemplate.send(destination, new Msg(message));System.out.println(String.format("目的地:%s ==> 发送消息:%s", destination, message));}}public void send(String destination, String message) throws Exception {this.message = message;this.destination = destination;run();}}
新建消息发送控制类SendController:
package net.xxpsw.demo.springboot.activemq.server;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import net.xxpsw.demo.springboot.activemq.server.send.Sender;@RestController@RequestMapping("activemq")public class SendController {@Autowiredprivate Sender sender;@RequestMapping("q1")public void q1(String dst, String msg) throws Exception {sender.send(dst, msg);}}

④ActiveMQ消息接收端

在boot-activemq-client项目中新建消息接收类Receiver:
package net.xxpsw.demo.springboot.activemq.client;import org.springframework.jms.annotation.JmsListener;import org.springframework.stereotype.Component;@Componentpublic class Receiver {@JmsListener(destination = "destination01")public void receiveMessage01(String message) {System.out.println(String.format("目的地:%s ==> 接收消息:%s", "destination01", message));}@JmsListener(destination = "destination02")public void receiveMessage02(String message) {System.out.println(String.format("目的地:%s ==> 接收消息:%s", "destination02", message));}}

3.使用ActiveMQ

①发送和接收队列消息

分别启动boot-activemq-client和boot-activemq-server的SpringBoot服务,接下来先查看ActiveMQ的管理后台,访问http://localhost:8161,查看Queues项如下(浏览器地址http://localhost:8161/admin/queues.jsp):

由上图可知,此时出现了两个消息队列,目的地分别为destination01和destination02,并且两个消息队列各有一个消费者。
接下来演示消息的发送和接收,浏览器访问如下地址:http://localhost:8090/activemq/q1?dst=destination01&msg=这是第1个队列消息,访问成功后,boot-activemq-server项目的控制台打印信息如下:

目的地:destination01 ==> 发送消息:这是第1个队列消息
boot-activemq-client项目的控制台打印信息如下:
目的地:destination01 ==> 接收消息:这是第1个队列消息
此时刷新ActiveMQ的管理后台可知,目的地为destination01的队列已成功发送了一条消息:

②发送和接收主题消息

SpringBoot的JMS默认发送的是队列消息,队列用于点对点的消息通信,点对点消息保证每条消息有唯一的发送者和接收者;而主题消息用于消息的发布订阅,允许多个消息接收者。若要发送主题消息,需要在boot-activemq-client和boot-activemq-server的配置文件中分别追加如下配置:
spring.jms.pub-sub-domain=true
重启boot-activemq-client和boot-activemq-server的SpringBoot服务,查看ActiveMQ管理后台Topics管理页(http://localhost:8161/admin/topics.jsp),可以看到新增了两个主题消息:

浏览器访问如下地址:http://localhost:8090/activemq/q1?dst=destination02&msg=这是第1个主题消息,访问成功后,boot-activemq-server项目的控制台打印信息如下:
目的地:destination02 ==> 发送消息:这是第1个主题消息
boot-activemq-client项目的控制台打印信息如下:
目的地:destination02 ==> 接收消息:这是第1个主题消息
此时刷新ActiveMQ的管理后台可知,主题为destination02的发布订阅完成了一次消息的发送和接收:

③设置消息访问密码

为了提高ActiveMQ的安全性,可以设置消息的访问密码,具体来说,编辑\conf\activemq.xml,在broker标签内追加如下配置:
<!-- user and password --><plugins>  <simpleAuthenticationPlugin>  <users>  <authenticationUser username="xxpsw" password="123456" groups="users,admins"/>  </users>  </simpleAuthenticationPlugin>  </plugins>  
重新启动ActiveMQ服务。添加上述配置后,ActiveMQ消息的发送和接收都要配置对应的用户密码,在boot-activemq-client和boot-activemq-server的配置文件中分别追加如下配置:
# 用户spring.activemq.user=xxpsw# 密码spring.activemq.password=123456
重启boot-activemq-client和boot-activemq-server项目的SpringBoot服务,即可安全地访问队列/主题消息。

原创粉丝点击