(转)RabbitMQ学习之spring整合发送异步消息(注解实现)
http://blog.csdn.net/zhu_tianwei/article/details/40919249
实现使用Exchange类型为DirectExchange. routingkey的名称默认为Queue的名称。注解实现异步发送消息。
1.生产者配置ProducerConfiguration.Java
- package cn.slimsmart.rabbitmq.demo.spring.async;
- import java.util.concurrent.atomic.AtomicInteger;
- import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
- import org.springframework.amqp.rabbit.connection.ConnectionFactory;
- import org.springframework.amqp.rabbit.core.RabbitTemplate;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.config.BeanPostProcessor;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor;
- import com.rabbitmq.client.AMQP;
- @Configuration
- public class ProducerConfiguration {
- // 指定队列名称 routingkey的名称默认为Queue的名称,使用Exchange类型为DirectExchange
- protected final String helloWorldQueueName = "spring-queue-async";
- // 创建链接
- @Bean
- public ConnectionFactory connectionFactory() {
- CachingConnectionFactory connectionFactory = new CachingConnectionFactory("192.168.36.102");
- connectionFactory.setUsername("admin");
- connectionFactory.setPassword("admin");
- connectionFactory.setPort(AMQP.PROTOCOL.PORT);
- return connectionFactory;
- }
- // 创建rabbitTemplate 消息模板类
- @Bean
- public RabbitTemplate rabbitTemplate() {
- RabbitTemplate template = new RabbitTemplate(connectionFactory());
- template.setRoutingKey(this.helloWorldQueueName);
- return template;
- }
- //创建一个调度
- @Bean
- public ScheduledProducer scheduledProducer() {
- return new ScheduledProducer();
- }
- @Bean
- public BeanPostProcessor postProcessor() {
- return new ScheduledAnnotationBeanPostProcessor();
- }
- static class ScheduledProducer {
- @Autowired
- private volatile RabbitTemplate rabbitTemplate;
- //自增整数
- private final AtomicInteger counter = new AtomicInteger();
- /**
- * 每3秒发送一条消息
- *
- * Spring3中加强了注解的使用,其中计划任务也得到了增强,现在创建一个计划任务只需要两步就完成了:
- 创建一个Java类,添加一个无参无返回值的方法,在方法上用@Scheduled注解修饰一下;
- 在Spring配置文件中添加三个<task:**** />节点;
- 参考:http://zywang.iteye.com/blog/949123
- */
- @Scheduled(fixedRate = 3000)
- public void sendMessage() {
- rabbitTemplate.convertAndSend("Hello World " + counter.incrementAndGet());
- }
- }
- }
2.生产者启动类Producer,java
- package cn.slimsmart.rabbitmq.demo.spring.async;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
- public class Producer {
- public static void main(String[] args) {
- new AnnotationConfigApplicationContext(ProducerConfiguration.class);
- }
- }
3.接收消息处理类ReceiveMsgHandler.java
- package cn.slimsmart.rabbitmq.demo.spring.async;
- public class ReceiveMsgHandler {
- public void handleMessage(String text) {
- System.out.println("Received: " + text);
- }
- }
4.消费者配置ConsumerConfiguration
- package cn.slimsmart.rabbitmq.demo.spring.async;
- import org.springframework.amqp.core.AmqpAdmin;
- import org.springframework.amqp.core.Queue;
- import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
- import org.springframework.amqp.rabbit.connection.ConnectionFactory;
- import org.springframework.amqp.rabbit.core.RabbitAdmin;
- import org.springframework.amqp.rabbit.core.RabbitTemplate;
- import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
- import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import com.rabbitmq.client.AMQP;
- @Configuration
- public class ConsumerConfiguration {
- // 指定队列名称 routingkey的名称默认为Queue的名称,使用Exchange类型为DirectExchange
- protected String springQueueDemo = "spring-queue-async";
- // 创建链接
- @Bean
- public ConnectionFactory connectionFactory() {
- CachingConnectionFactory connectionFactory = new CachingConnectionFactory(
- "192.168.36.102");
- connectionFactory.setUsername("admin");
- connectionFactory.setPassword("admin");
- connectionFactory.setPort(AMQP.PROTOCOL.PORT);
- return connectionFactory;
- }
- // 创建rabbitAdmin 代理类
- @Bean
- public AmqpAdmin amqpAdmin() {
- return new RabbitAdmin(connectionFactory());
- }
- // 创建rabbitTemplate 消息模板类
- @Bean
- public RabbitTemplate rabbitTemplate() {
- RabbitTemplate template = new RabbitTemplate(connectionFactory());
- // The routing key is set to the name of the queue by the broker for the
- // default exchange.
- template.setRoutingKey(this.springQueueDemo);
- // Where we will synchronously receive messages from
- template.setQueue(this.springQueueDemo);
- return template;
- }
- //
- // Every queue is bound to the default direct exchange
- public Queue helloWorldQueue() {
- return new Queue(this.springQueueDemo);
- }
- @Bean
- public SimpleMessageListenerContainer listenerContainer() {
- SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
- container.setConnectionFactory(connectionFactory());
- container.setQueueNames(this.springQueueDemo);
- container.setMessageListener(new MessageListenerAdapter(
- new ReceiveMsgHandler()));
- return container;
- }
- }
5.消费者启动类Consumer.java
- package cn.slimsmart.rabbitmq.demo.spring.async;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
- public class Consumer {
- public static void main(String[] args) {
- new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
- }
- }
启动接收消息,再发送消息
- Received: Hello World 1
- Received: Hello World 2
- Received: Hello World 3
- Received: Hello World 4
- Received: Hello World 5
- Received: Hello World 6
- Received: Hello World 7
- ......
若报spring-queue-async消息队列不存在,请在控制台添加。
(转)RabbitMQ学习之spring整合发送异步消息(注解实现)相关推荐
- (转) RabbitMQ学习之spring整合发送异步消息
http://blog.csdn.net/zhu_tianwei/article/details/40919031 实现使用Exchange类型为DirectExchange. routingkey的 ...
- (转) RabbitMQ学习之spring整合发送同步消息(注解实现)
http://blog.csdn.net/zhu_tianwei/article/details/40918477 上一篇文章通过xml配置rabbitmq的rabbitTemplate,本节将使用注 ...
- (转)RabbitMQ学习之spring整合发送同步消息
http://blog.csdn.net/zhu_tianwei/article/details/40890543 以下实现使用Exchange类型为DirectExchange. routingke ...
- Dubbo学习记录(八) -- Spring整合Dubbo中@Reference注解解析原理
Spring整合Dubbo中@Reference注解解析原理 @Reference: 可以用在属性或者方法, 意味着需要引用某个Dubbo服务, 那么Dubbo整合Spring后, 我很好奇怎么把这个 ...
- Spring整合ActiveMQ完成消息队列MQ编程
<–start–> 第一步:新建一个maven,将工程命名为activeMQ_spring.在pom.xml文件中导入相关jar包. ①spring开发和测试相关的jar包: spring ...
- Spring整合JMS(二)——消息监听器
消息监听器 在Spring整合JMS的应用中我们在定义消息监听器的时候一共能够定义三种类型的消息监听器,各自是MessageListener.SessionAwareMessageListener和M ...
- RabbitMQ学习之spring配置文件rabbit标签的使用
下面我们通过一个实例看一下rabbit的使用. 1.实现一个消息监听器ReceiveMessageListener.Java [java] view plaincopy print? package ...
- 文件用户Apache shiro学习笔记+ spring整合shiro (一)
改章节朋友在青岛游玩的时候突然想到的...这两天就有想写几篇关于文件用户的博客,所以回家到之后就奋笔疾书的写出来发表了 Apache Shiro官网:http://shiro.apache.org/ ...
- java监控activemq,ActiveMQ与Spring整合-监听消息
本课程全程使用目前比较流行的开发工具idea进行开发,涉及到目前互联网项目中常用的高并发解决方案技术, 如 dubbo,redis,solr,freemarker,activeMQ,springBo ...
最新文章
- python使用正則表達式
- 文献读的越多,离原创越远
- arm-none-eabi-gcc.exe -v
- 使用CXF 来发布一个 service
- php5.6 mysql被重置_php5.6连接mysql8出现错误解决方法
- Android pm命令(持续更新中...)
- 43. Element hasAttributes() 方法
- ES5 对象的扩展(Object.preventExtensions)、密封(Object.seal)和冻结(Object.freeze)
- charles4.0破解和手机抓包
- 计算机画图卡通,windows画图工具怎么画卡通头像?
- 如何彻底卸载office!!
- 算法笔记学习day1(第二章)
- spring扫描出现Annotation-specified bean name 'userService' for bean class [com.test.service.UserService]
- 魔兽地图服务器修改,魔兽争霸3冰封王座地图编辑器修改无限人口的方法
- arcgis api for javascript 的swipe的使用
- WIN10任务栏卡死,鼠标一直转圈(亲测有效)
- Bitmap 图片缩放
- Design Compiler工具学习笔记(7)
- SecureCRT 注册机使用方法
- MySQL连接速度太慢_mysql-连接速度非常慢(1秒)