内容简介:在spring-boot中使用ActiveMQ相当的简单引入依赖添加配置文件
在spring-boot中使用ActiveMQ相当的简单
引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency>
添加配置文件
spring: activemq: broker-url: tcp://localhost:61616
spring-boot里也可以进行数据连接池的配置,这个之前几篇博客已经都配置好了,这里就不做配置了
消息提供者
@Service("producer") public class Producer { @Autowired private JmsMessagingTemplate jmsTemplate; // 发送消息,destination是发送到的队列,message是待发送的消息 public void sendMessage(Destination destination, String message) { jmsTemplate.convertAndSend(destination, message); } }
消息消费者1
@Component public class Consumer1 { // 使用JmsListener配置消费者监听的队列,其中text是接收到的消息 @JmsListener(destination = "amq-demo") public void receiveQueue(String text) { System.out.println("Consumer1: " + text); } }
消息消费者2
@Component public class Consumer2 { // 使用JmsListener配置消费者监听的队列,其中text是接收到的消息 @JmsListener(destination = "amq-demo") public void receiveQueue(String text) { System.out.println("Consumer2: " + text); } }
消息消费者3
@Component public class Consumer3 { @JmsListener(destination = "amq-demo2") public void consumerMessage(String text) { System.out.println("收到来自amq-demo2队列的消息: " + text); } }
test方法
@RunWith(SpringRunner.class) @SpringBootTest public class DemoApplicationTests { @Autowired private Producer producer; @Test public void contextLoads() { Destination destination = new ActiveMQQueue("amq-demo"); for (int i = 1; i <= 5; i++) { producer.sendMessage(destination, "Producer消息" + i); } } }
运行结果
说明
- 提供者发送消息到ActiveMQ服务器里
- 消费者1和消费者2同时消费数据,它们不会重复消费,既有一个消费者把消息消费了,其它消费者就不会再消费这条数据了
- 消费者2消费数据的同时还将消息重新发出(这里的意思应该是消费者2临时充当了消息提供者的角色了,这个功能可以处理一些复杂的,一个消费者处理不完的消息,或者处理一些消息后,还有后续的操作的业务)
- 消费者3监听消费者2发出的消息然后进行消费
注意,运行test方法进行测试前要先启动 ActiveMQ
原文链接:
以上所述就是小编给大家介绍的《ActiveMQ学习-与spring整合,在spring-boot中使用 (5)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- SpringBoot Kafka 整合使用
- SpringBoot RabbitMQ 整合使用
- SpringBoot ActiveMQ 整合使用
- SpringBoot RabbitMQ 整合使用
- Redis和Lua初步整合使用
- redis与spring整合使用的步骤实例教程
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
程序设计语言理论基础
米切尔 / 电子工业出版社 / 2006-11 / 68.00元
本书提出了一个框架,用于分析程序设计语言的语法、操作和语义性质,该框架基于称为类型化λ演算的数学系统。λ演算的主要特色是对于函数和其他可计算的值的一种记法,以及一个等式逻辑和用于表达式求值的一组规则。本书中最简单的系统是称为泛代数的一个等式系统,它可以用来公理化和分析通常用于程序设计的许多数据类型。可作为理论计算机科学、软件系统和数学专业的大学本科高年级或者研究生初始学习阶段的教材,同时也适合用于......一起来看看 《程序设计语言理论基础》 这本书的介绍吧!