SpringBoot整合ActiveMQ
SpringBoot能很好的整合ActiveMQ,底层封装了很多繁琐的操作,让我们能够很简单的进行配置就能使用MQ
整合流程:
队列模式(queue):
1.加入依赖到pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency>
2.配置yml
server: port: 7777 spring: activemq: broker-url: tcp://127.0.0.1:61616 #MQ服务器地址 user: admin password: admin jms: pub-sub-domain: false #false = queue true = topic ,不写默认为队列 #定义队列名称 myqueue: queue-test
3.编写配置类bean,目的是灵活的获取信息
@Component @EnableJms public class configBean { @Value("${myqueue}") //获取队列的名字 private String myQueue; @Bean //作为一个bean,方便注入 public Queue queue(){ return new ActiveMQQueue(myQueue); //加入队列 } }
4.编写生产者代码
@Component public class Producer { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; //SpringBoot提供的模板,功能更加丰富 @Autowired private Queue queue; //自动注入configBean里面的Queue
//手动投递,点以下投一下 public void produceMsg(){ jmsMessagingTemplate.convertAndSend(queue,"boot-test"+ UUID.randomUUID().toString().substring(0,17).replace("-","")); } //间隔定投 @Scheduled(fixedDelay = 3000) //三秒钟往队列里投递一次消息,加上此注解需要在主启动类上加上启动注解@EnableScheduling public void produceMsgScheduled(){ jmsMessagingTemplate.convertAndSend(queue,"间隔定投 "+UUID.randomUUID().toString().substring(0,17).replace("-","")); System.out.println("定时投递消息 "); } }
5.消费者接收消息
消费者这边的配置和生产者差不多一样,pom.xml一样,yml修改server.port就行,其余一样
@Component
public class Consumer {
@JmsListener(destination = "${myqueue}") //监听注解,时刻监听生产者是否发了消息过来
public void receiveMsg(TextMessage textMessage) throws JMSException {
System.out.println("接收到的消息为: "+textMessage.getText());
}
}
主题模式(Topic):
主题模式只需要将queue改为Topic就行
1.yml
server: port: 7777 spring: activemq: broker-url: tcp://127.0.0.1:61616 #MQ服务器地址 user: admin password: admin jms: pub-sub-domain: true #false = queue true = topic ,不写默认为队列 #定义主题名称 myTopic: topic-test
2.config
@Component @EnableJms public class configBean { @Value("${myTopic}") private String topic; @Bean public Topic topic(){ //主题 return new ActiveMQTopic(topic); } }
3.Producer
@Component public class Producer { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; @Autowired private Topic topic; //config里面的Topic public void produceMsg(){ jmsMessagingTemplate.convertAndSend(topic,"boot-test"+ UUID.randomUUID().toString().substring(0,17).replace("-","")); } //间隔定投 @Scheduled(fixedDelay = 3000) //三秒钟往队列里投递一次消息 public void produceMsgScheduled(){ jmsMessagingTemplate.convertAndSend(topic,"间隔定投 "+UUID.randomUUID().toString().substring(0,17).replace("-","")); System.out.println("定时投递消息 "); } }
3.Consmer
@Component public class Consumer { @JmsListener(destination = "${myTopic}") public void receiveMsg(TextMessage textMessage) throws JMSException { System.out.println("接收到的消息为: "+textMessage.getText()); } }
生命不止,折腾不息
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端