springboot 集成activeMQ实现消息队列和双向队列
2018-01-31 00:00
1501 查看
一、引入依赖配置
二、application.properties配置文件中新增配置
三、新建消息生产者类
四、新建消息消费者ABC
五、新建接口用来测试
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> </dependency>
二、application.properties配置文件中新增配置
# ActiveMQ-------------------------------------------------------------------------------------------------------------- # Specify if the default broker URL should be in memory. Ignored if an explicit broker has been specified. # 默认为true表示使用内存的activeMQ,不需要安装activeMQ server #spring.activemq.in-memory=false # URL of the ActiveMQ broker. Auto-generated by default. For instance `tcp://localhost:61616` spring.activemq.broker-url=tcp://localhost:61616 # Login user of the broker. spring.activemq.user=admin # Login password of the broker. spring.activemq.password=admin # Trust all packages. #spring.activemq.packages.trust-all=false # Comma-separated list of specific packages to trust (when not trusting all packages). #spring.activemq.packages.trusted= # See PooledConnectionFactory. #spring.activemq.pool.configuration.*= # Whether a PooledConnectionFactory should be created instead of a regular ConnectionFactory. spring.activemq.pool.enabled=true # Maximum number of pooled connections. spring.activemq.pool.max-connections=50 # Connection expiration timeout in milliseconds. spring.activemq.pool.expiry-timeout=10000 # Connection idle timeout in milliseconds. spring.activemq.pool.idle-timeout=30000 # 如果为True,则是Topic;如果是false或者默认,则是queue。 spring.jms.pub-sub-domain=false
三、新建消息生产者类
@Service("producer") public class Producer { @Resource private JmsTemplate jmsTemplate; /** * 发送消息 * * @param destination 发送到的队列 * @param message 待发送的消息 */ public void convertAndSend(Destination destination, final String message) { jmsTemplate.convertAndSend(destination, message); } }
四、新建消息消费者ABC
@Component public class ConsumerA { /** * 使用JmsListener配置消费者监听的队列 * * @param text 接收到的消息 */ @JmsListener(destination = "suimh_queue") public void receiveQueue(String text) { System.out.println("Consumer-A : 收到的报文为:" + text); } }
@Component public class ConsumerB { /** * 使用JmsListener配置消费者监听的队列 * * @param text 接收到的消息 */ @JmsListener(destination = "suimh_queue") @SendTo("out.queue") public String receiveQueue(String text) { System.out.println("Consumer-B : 收到的报文为:" + text); return text; } }
@Component public class ConsumerC { /** * 使用JmsListener配置消费者监听的队列 * * @param text 接收到的消息 */ @JmsListener(destination = "out.queue") public void consumerMessage(String text) { System.out.println("Consumer-C : 从out.queue队列收到的回复报文为:" + text); } }
五、新建接口用来测试
/** * activeMQ测试方法(queue队列模式) * 如果想用Topic发布订阅模式,需要新建ActiveMQTopic实例,并修改配置文件spring.jms.pub-sub-domain=true * * @return String */ @GetMapping(value = "/activeMqSendMes") @ResponseBody public String activeMqSendMes() { int num = 10; try { Destination destinationQueue = new ActiveMQQueue("suimh_queue"); for (int i = 1; i <= num; i++) { producer.convertAndSend(destinationQueue, "这是queueProducer发送的第" + i + "个消息!"); } return "activeMQ生产成功!"; } catch (Exception e) { return "activeMQ生产失败!"; } }
相关文章推荐
- spring boot Rabbitmq集成,延时消息队列实现
- spring boot 集成activeMQ消息队列
- ActiveMQ消息中间件 原理详解 &&附demo实现、以及与springboot集成的demo
- spring boot Rabbitmq集成,延时消息队列实现
- Spring和ActiveMQ集成实现队列消息以及PUB/SUB模型
- SpringBoot利用redis集成消息队列的方法
- SpringBoot非官方教程 | 第十四篇:在springboot中用redis实现消息队列
- SpringBoot非官方教程 | 第十四篇:在springboot中用redis实现消息队列
- Spring Boot + RabbitMQ 实现消息队列场景
- 【Demo】springboot微服务+activemq消息中间件集成
- 在Spring下集成ActiveMQ 实现点到点的消息发送
- Spring整合ActiveMQ实现简单的消息队列
- spring boot整合activeMQ,实现ptp和topic两者消息模式
- Spring Boot教程(二十四)在springboot中用redis实现消息队列
- [置顶] 【Excel_To_DB】SpringBoot+EasyPoi+Redis消息队列实现Excel批量异步导入数据库(一)
- SpringBoot非官方教程 | 第十四篇:在springboot中用redis实现消息队列
- redis 消息队列发布订阅模式spring boot实现
- SpringBoot非官方教程 | 第十四篇:在springboot中用redis实现消息队列
- Java ActiveMQ简介以及springboot集成activeMQ实现消息队列监听以及实现MQ延迟
- 企业级 SpringBoot 教程 (十四) 在springboot中用redis实现消息队列