SpringBoot整合ActiveMQ
首先了解以下什么是消息队列(Message Queue),消息队列是一种进程中或者线程间的异步通信方式,使用消息队列,消息生产者在产生消息后,会将消息保存在消息队列中,直到消息消费者来取走它,即消息的发送者和接收者不需要同时与消息队列交互。使用消息队列可以有效实现服务的解耦,并提高系统的可靠性以及扩展性
首先我们需要先安装 ActiveMQ ,我安装的是 Windows 版本的
ActiveMQ 官网下载路径:http://activemq.apache.org/download.html
ActiveMQ 安装可参考: https://blog.csdn.net/mr_haixin/article/details/80418204
等安装好 ActiveMQ 后,打开cmd命令行进入 ActiveMQ 的安装目录,执行此命令进行启动 ActiveMQ,启动成功后如下图所示:
bin\activemq start
AcitveMQ 启动成功后,不要关闭cmd窗口,然后访问 http://localhost:8161/admin/ 进入 ActiveMQ Console 就表示ActiveMQ启动成功了,如下所示:
8161是 ActiveMQ的默认端口号,它的账号和密码默认都是 admin
整合 Spring Boot
1.首先添加 ActiveMQ 的相关依赖,代码如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
然后在 application.properties 中进行连接配置,代码如下:
spring.activemq.broker-url=tcp://127.0.0.1:61616 //配置broker地址,默认端口是61616
spring.activemq.packages.trust-all=true //配置所有信任的包
spring.activemq.user=admin //账号
spring.activemq.password=admin //密码
2.接下来在 项目配置类中 提供一个消息队列的Bean,该 Bean 的实例就由 ActiveMQ 提供,代码如下:
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import javax.jms.Queue;
@SpringBootApplication
public class ActivemqApplication {
public static void main(String[] args) {
SpringApplication.run(ActivemqApplication.class, args);
}
@Bean
Queue queue() {
return new ActiveMQQueue("amq");
}
}
3.在创建一个 JMS 组件来完成消息的发送和接收,代码如下:
import com.sang.activemq.entity.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;
import javax.jms.Queue;
@Component
public class JmsComponent {
@Autowired //提供一个 JMS 消息发送模板
JmsMessagingTemplate messagingTemplate;
@Autowired
Queue queue;
public void send(Message msg) {
messagingTemplate.convertAndSend(this.queue,msg);
}
@JmsListener(destination = "amq")
public void receive(Message msg) {
System.out.println("receive:" + msg);
}
}
Message实体类
package com.sang.activemq.entity;
import java.io.Serializable;
import java.util.Date;
public class Message implements Serializable {
private String content;
private Date date;
public Message(){
super();
}
public Message(String content, Date date) {
this.content = content;
this.date = date;
}
@Override
public String toString() {
return "Message{" +
"content='" + content + '\'' +
", date=" + date +
'}';
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
4.最后进行测试
编写测试类,完成消息发送测试,代码如下:
在测试类中注入 JmsComponent 组件,然后调用组件的 send 方法发送一个 Message 对象。首先确定 ActiveMQ 启动,然后启动 SpringBoot 项目,最后执行单元测试,观察 Spring Boot 项目日志,如下图所示
@SpringBootTest
class ActivemqApplicationTests {
@Autowired
JmsComponent jmsComponent;
@Test
public void contextLoads() {
Message msg = new Message();
msg.setContent("hello jms!");
msg.setDate(new Date());
jmsComponent.send(msg);
}
}
这些就是 SpringBoot 整合ActiveMQ的简单使用!!!