1、在pom.xml中添加依赖包
<!-- 整合activemq -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
2、创建全局配置文件(application.properties)
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.in-memory=true
spring.activemq.pool.enabled=false
3、创建测试类(MsgHandler.java)
package com.jeff.receive;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Component
public class MsgHandler {
// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
@JmsListener(destination = "jeff_queue")
public void receiveQueue(String text) {
System.out.println("####消息服务收到的报文为:" + text);
}
}
4、启动springboot项目,即可监听MQ的消息
5、查看MQ的消息队列,消息已消费