1 首先准备一台虚拟机 用于搭建activemq ,activemq 安装就不多讲了 ,这里 推荐使用docker安装 贼方便。 我这边用docker 开启activemq ,输入docker ps 如果正确启动 会有如下的输出信息。
2 然后本地window 上输入activemq 的访问地址 (我这里是虚拟机ip :8162)(注意 需要关闭 linux 的防火墙)启动成功 会显示如下的管理页面 默认用户名密码是admin admin
3 接下来开始整合 springboot
3.1 queue 模式
1 新建一个springboot 工程 引入activemq 的pom 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<version>1.5.0.RELEASE</version>
</dependency>
<!--消息队列连接池-->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.15.0</version>
</dependency>
2 配置yaml 文件
server:
port: 8081
spring:
activemq:
broker-url: tcp://http://192.168.171.128:61617
user: admin
password: admin
jms:
pub-sub-domain: false #false为Queue,true为Topic,默认false-Queue
myqueue: activemq-queue
mytopic: activemq-topic
3 mq配置类
package com.example.config;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import javax.jms.Queue;
import javax.jms.Topic;
@Configuration
@EnableJms
public class ConfigBean {
@Value("$(myqueue)")
private String myQueue;
@Value("$(mytopic)")
private String myTopic;
@Bean
public Queue queue(){
return new ActiveMQQueue(myQueue);
}
@Bean
public Topic topic(){
return new ActiveMQTopic(myTopic);
}
}
4 队列生产者和消费者
package com.example.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.jms.Queue;
@RestController
public class ProviderController {
@Autowired
private Queue queue;
//注入springboot封装的工具类
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@RequestMapping("send")
public void send(String msg) {
jmsMessagingTemplate.convertAndSend(queue, msg);
}
}
package com.example.web;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConsumerController {
@JmsListener(destination="${myqueue}")
public void readActiveQueue(String message) {
System.out.println("接受到:" + message);
}
}
5 启动springboot 在浏览器输入http://localhost:8081/send?msg="abc" 控制台和mq 管理页面显示如下 这 说明已经成功生产和消费了
3.2 topic 模式
将yaml 中的
jms:
pub-sub-domain: false 改为true
在原来的生产者类中加入
@Autowired
private Topic topic;
@RequestMapping("/topicsendmsg")
public void sendmsg(String msg) {
System.out.println("发送消息到MQ:" + msg);
// 指定消息发送的目的地及内容
this.jmsMessagingTemplate.convertAndSend(this.topic, msg);
}
消费者类加入 这里指定两个消费者
@JmsListener(destination="${mytopic}")
public void readActiveTopic(String message) {
System.out.println("接受到:" + message);
}
@JmsListener(destination="${mytopic}")
public void readActiveQueue1(String message) {
System.out.println("接受到:" + message);
}
同样启动springboot 浏览器输入http://localhost:8081/topicsendmsg?msg="abc"
控制台会显示
mq 控制台会显示
ok