由于工作需求需要讲activemq集成到springboot项目中
第一步
需要先在pom.xml文件中讲依赖导入,我这里是新建里一个模块
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.messaginghub</groupId>
<artifactId>pooled-jms</artifactId>
</dependency>
第二步
是在配置文件中配置mq的相关参数,在application.yml文件中
这里要注意,activemq是配置到spring的后面,空格要搞清楚
activemq:
# 设置连接的activemq服务器
broker-url: failover:(tcp://xx.xx.xx.xx:61616,mqtt://xx.xx.xx.xx:1883)
#结束等待时长
close-timeout: 10s
# true 表示使用内置的MQ,false则连接服务器。
in-memory: true
# 是否在回滚回滚消息之前停止消息传递。这意味着当启用此命令时,消息顺序不会被保留
non-blocking-redelivery: false
# 等待消息发送响应的时间。设置为0等待永远
send-timeout: 0
user: admin
password: admin
packages:
# 是否信任所有包
trust-all: true
# 要信任的特定包的逗号分隔列表(当不信任所有包时)
trusted:
pool:
# 当连接请求和池满时是否阻塞。设置false会抛“JMSException异常”。
block-if-full: true
# 如果池仍然满,则在抛出异常前阻塞时间。
block-if-full-timeout: -1ms
# 是否在启动时创建连接。可以在启动时用于加热池
#create-connection-on-startup: true
# 是否用Pooledconnectionfactory代替普通的ConnectionFactory。
enabled: false
# 连接过期超时。
#expiry-timeout: 0ms
# 连接空闲超时
idle-timeout: 30ms
# 连接池最大连接数
max-connections: 1
# 每个连接的有效会话的最大数目。
maximum-active-session-per-connection: 500
#当有"JMSException"时尝试重新连接
#reconnect-on-exception: true
# 在空闲连接清除线程之间运行的时间。当为负数时,没有空闲连接驱逐线程运行。
time-between-expiration-check: -1ms
# 是否只使用一个MessageProducer
use-anonymous-producers: true
最后是这个jms配置,也是需要配置在spring下面,用来控制activemq是否可以使用topic模式通讯
jms:
#默认情况下activemq提供的是queue模式,若要使用topic模式需要配置下面配置
pub-sub-domain: true
第三步
编写测试类
①创建QueueMessageService类,用来注册消息队列
/**
* @use:
* @Author: xiaomiduizhang
* @DateTime: 2021/12/21 21:57
* @Description: V1.0
*/
@Component
public class QueueMessageService {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
/**
* 使用queue模式注册队列
* @param queue
* @param msg
*/
public void setQueue(String queue,String msg){
jmsMessagingTemplate.convertAndSend(new ActiveMQQueue(queue),msg);
}
/**
* 使用topic模式注册队列
* @param topic
* @param msg
*/
public void setTopic(String topic,String msg) {
jmsMessagingTemplate.convertAndSend(new ActiveMQTopic(topic),msg);
}
}
②创建ProducerController类,用来接收接口过来的数据,并传递到消息队列中
/**
* @use: 生产者队列
* @Author: xiaomiduizhang
* @DateTime: 2021/12/21 22:02
* @Description: V1.0
*/
@RestController
@RequestMapping("message")
public class ProducerController {
@Autowired
private QueueMessageService queueMessageService;
/**
* 消息生产者
*/
@GetMapping("sendmsg")
public void sendmsg(String msg) {
// 指定消息发送的目的地及内容
queueMessageService.setQueue("queue",msg);
}
@GetMapping("sendmsgx")
public void sendmsgtox(String key,String msg){
// 由生产者指定消息发送的目的地及内容
queueMessageService.setQueue(key,msg);
}
@GetMapping("send")
public void send(String msg) {
// 指定消息发送的目的地及内容
//System.out.println(new Date().toString());
queueMessageService.setTopic("topic",msg);
}
}
③创建ConsumerController类,用来接收来自消息队列的消息
/**
* @use: 消费者客户控制器
* @Author: xiaomiduizhang
* @DateTime: 2021/12/21 22:04
* @Description: V1.0
*/
@RestController
public class ConsumerController {
/**
* 监听队列
* @param message
*/
@JmsListener(destination="queue")
public void readActiveQueue(String message) {
System.out.println("queue模式接收1:" + message);
//TODO something
}
@JmsListener(destination = "queue_b")
public void readQueue(String message){
System.out.println("queue模式接收2:"+message);
}
@JmsListener(destination = "queue_byte")
public void readQueueByte(byte[] message){
System.out.println("queue模式接收3:"+message);
}
@JmsListener(destination = "topic")
public void receiveMessage(String message) throws InterruptedException {
System.out.println(new Date().toString()+"topic接收:"+message);
}
@JmsListener(destination = "topic")
public void readResponse(String message) throws InterruptedException {
System.out.println(new Date().toString()+"topic1接收:"+message);
}
}
第四步
验证测试
使用apiPost作为接口调试工具,启动项目后向接口发送一条数据
接收到的结果打印到控制台如下
总结
①in-memory: true 配置根本不管用,不管是否设置为true,只要配置了broker-url: failover:(tcp://xx.xx.xx.xx:61616,mqtt://xx.xx.xx.xx:1883)都会优先连接外部activemq服务器,而不是使用内部的。不知道是不是我用错了,有知道打大神请在评论告诉我。(使用内部服务把broker-url配置注释即可)
② jms 中 pub-sub-domain 参数配置queue模式和topic模式只能选择其中一种使用,设为true时,queue模式不可用