http://blog.csdn.net/zhu_tianwei/article/details/40919249

实现使用Exchange类型为DirectExchange. routingkey的名称默认为Queue的名称。注解实现异步发送消息。

1.生产者配置ProducerConfiguration.Java

[java] view plaincopy print?
  1. package cn.slimsmart.rabbitmq.demo.spring.async;
  2. import java.util.concurrent.atomic.AtomicInteger;
  3. import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
  4. import org.springframework.amqp.rabbit.connection.ConnectionFactory;
  5. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.beans.factory.config.BeanPostProcessor;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.scheduling.annotation.Scheduled;
  11. import org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor;
  12. import com.rabbitmq.client.AMQP;
  13. @Configuration
  14. public class ProducerConfiguration {
  15. // 指定队列名称 routingkey的名称默认为Queue的名称,使用Exchange类型为DirectExchange
  16. protected final String helloWorldQueueName = "spring-queue-async";
  17. // 创建链接
  18. @Bean
  19. public ConnectionFactory connectionFactory() {
  20. CachingConnectionFactory connectionFactory = new CachingConnectionFactory("192.168.36.102");
  21. connectionFactory.setUsername("admin");
  22. connectionFactory.setPassword("admin");
  23. connectionFactory.setPort(AMQP.PROTOCOL.PORT);
  24. return connectionFactory;
  25. }
  26. // 创建rabbitTemplate 消息模板类
  27. @Bean
  28. public RabbitTemplate rabbitTemplate() {
  29. RabbitTemplate template = new RabbitTemplate(connectionFactory());
  30. template.setRoutingKey(this.helloWorldQueueName);
  31. return template;
  32. }
  33. //创建一个调度
  34. @Bean
  35. public ScheduledProducer scheduledProducer() {
  36. return new ScheduledProducer();
  37. }
  38. @Bean
  39. public BeanPostProcessor postProcessor() {
  40. return new ScheduledAnnotationBeanPostProcessor();
  41. }
  42. static class ScheduledProducer {
  43. @Autowired
  44. private volatile RabbitTemplate rabbitTemplate;
  45. //自增整数
  46. private final AtomicInteger counter = new AtomicInteger();
  47. /**
  48. * 每3秒发送一条消息
  49. *
  50. * Spring3中加强了注解的使用,其中计划任务也得到了增强,现在创建一个计划任务只需要两步就完成了:
  51. 创建一个Java类,添加一个无参无返回值的方法,在方法上用@Scheduled注解修饰一下;
  52. 在Spring配置文件中添加三个<task:**** />节点;
  53. 参考:http://zywang.iteye.com/blog/949123
  54. */
  55. @Scheduled(fixedRate = 3000)
  56. public void sendMessage() {
  57. rabbitTemplate.convertAndSend("Hello World " + counter.incrementAndGet());
  58. }
  59. }
  60. }

2.生产者启动类Producer,java

[java] view plaincopy print?
  1. package cn.slimsmart.rabbitmq.demo.spring.async;
  2. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  3. public class Producer {
  4. public static void main(String[] args) {
  5. new AnnotationConfigApplicationContext(ProducerConfiguration.class);
  6. }
  7. }

3.接收消息处理类ReceiveMsgHandler.java

[java] view plaincopy print?
  1. package cn.slimsmart.rabbitmq.demo.spring.async;
  2. public class ReceiveMsgHandler {
  3. public void handleMessage(String text) {
  4. System.out.println("Received: " + text);
  5. }
  6. }

4.消费者配置ConsumerConfiguration

[java] view plaincopy print?
  1. package cn.slimsmart.rabbitmq.demo.spring.async;
  2. import org.springframework.amqp.core.AmqpAdmin;
  3. import org.springframework.amqp.core.Queue;
  4. import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
  5. import org.springframework.amqp.rabbit.connection.ConnectionFactory;
  6. import org.springframework.amqp.rabbit.core.RabbitAdmin;
  7. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  8. import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
  9. import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
  10. import org.springframework.context.annotation.Bean;
  11. import org.springframework.context.annotation.Configuration;
  12. import com.rabbitmq.client.AMQP;
  13. @Configuration
  14. public class ConsumerConfiguration {
  15. // 指定队列名称 routingkey的名称默认为Queue的名称,使用Exchange类型为DirectExchange
  16. protected String springQueueDemo = "spring-queue-async";
  17. // 创建链接
  18. @Bean
  19. public ConnectionFactory connectionFactory() {
  20. CachingConnectionFactory connectionFactory = new CachingConnectionFactory(
  21. "192.168.36.102");
  22. connectionFactory.setUsername("admin");
  23. connectionFactory.setPassword("admin");
  24. connectionFactory.setPort(AMQP.PROTOCOL.PORT);
  25. return connectionFactory;
  26. }
  27. // 创建rabbitAdmin 代理类
  28. @Bean
  29. public AmqpAdmin amqpAdmin() {
  30. return new RabbitAdmin(connectionFactory());
  31. }
  32. // 创建rabbitTemplate 消息模板类
  33. @Bean
  34. public RabbitTemplate rabbitTemplate() {
  35. RabbitTemplate template = new RabbitTemplate(connectionFactory());
  36. // The routing key is set to the name of the queue by the broker for the
  37. // default exchange.
  38. template.setRoutingKey(this.springQueueDemo);
  39. // Where we will synchronously receive messages from
  40. template.setQueue(this.springQueueDemo);
  41. return template;
  42. }
  43. //
  44. // Every queue is bound to the default direct exchange
  45. public Queue helloWorldQueue() {
  46. return new Queue(this.springQueueDemo);
  47. }
  48. @Bean
  49. public SimpleMessageListenerContainer listenerContainer() {
  50. SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
  51. container.setConnectionFactory(connectionFactory());
  52. container.setQueueNames(this.springQueueDemo);
  53. container.setMessageListener(new MessageListenerAdapter(
  54. new ReceiveMsgHandler()));
  55. return container;
  56. }
  57. }

5.消费者启动类Consumer.java

[java] view plaincopy print?
  1. package cn.slimsmart.rabbitmq.demo.spring.async;
  2. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  3. public class Consumer {
  4. public static void main(String[] args) {
  5. new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
  6. }
  7. }

启动接收消息,再发送消息

[sql] view plaincopy print?
  1. Received: Hello World 1
  2. Received: Hello World 2
  3. Received: Hello World 3
  4. Received: Hello World 4
  5. Received: Hello World 5
  6. Received: Hello World 6
  7. Received: Hello World 7
  8. ......

若报spring-queue-async消息队列不存在,请在控制台添加。

(转)RabbitMQ学习之spring整合发送异步消息(注解实现)相关推荐

  1. (转) RabbitMQ学习之spring整合发送异步消息

    http://blog.csdn.net/zhu_tianwei/article/details/40919031 实现使用Exchange类型为DirectExchange. routingkey的 ...

  2. (转) RabbitMQ学习之spring整合发送同步消息(注解实现)

    http://blog.csdn.net/zhu_tianwei/article/details/40918477 上一篇文章通过xml配置rabbitmq的rabbitTemplate,本节将使用注 ...

  3. (转)RabbitMQ学习之spring整合发送同步消息

    http://blog.csdn.net/zhu_tianwei/article/details/40890543 以下实现使用Exchange类型为DirectExchange. routingke ...

  4. Dubbo学习记录(八) -- Spring整合Dubbo中@Reference注解解析原理

    Spring整合Dubbo中@Reference注解解析原理 @Reference: 可以用在属性或者方法, 意味着需要引用某个Dubbo服务, 那么Dubbo整合Spring后, 我很好奇怎么把这个 ...

  5. Spring整合ActiveMQ完成消息队列MQ编程

    <–start–> 第一步:新建一个maven,将工程命名为activeMQ_spring.在pom.xml文件中导入相关jar包. ①spring开发和测试相关的jar包: spring ...

  6. Spring整合JMS(二)——消息监听器

    消息监听器 在Spring整合JMS的应用中我们在定义消息监听器的时候一共能够定义三种类型的消息监听器,各自是MessageListener.SessionAwareMessageListener和M ...

  7. RabbitMQ学习之spring配置文件rabbit标签的使用

    下面我们通过一个实例看一下rabbit的使用. 1.实现一个消息监听器ReceiveMessageListener.Java [java] view plaincopy print? package  ...

  8. 文件用户Apache shiro学习笔记+ spring整合shiro (一)

    改章节朋友在青岛游玩的时候突然想到的...这两天就有想写几篇关于文件用户的博客,所以回家到之后就奋笔疾书的写出来发表了 Apache Shiro官网:http://shiro.apache.org/ ...

  9. java监控activemq,ActiveMQ与Spring整合-监听消息

    本课程全程使用目前比较流行的开发工具idea进行开发,涉及到目前互联网项目中常用的高并发解决方案技术, 如  dubbo,redis,solr,freemarker,activeMQ,springBo ...

最新文章

  1. python使用正則表達式
  2. 文献读的越多,离原创越远
  3. arm-none-eabi-gcc.exe -v
  4. 使用CXF 来发布一个 service
  5. php5.6 mysql被重置_php5.6连接mysql8出现错误解决方法
  6. Android pm命令(持续更新中...)
  7. 43. Element hasAttributes() 方法
  8. ES5 对象的扩展(Object.preventExtensions)、密封(Object.seal)和冻结(Object.freeze)
  9. charles4.0破解和手机抓包
  10. 计算机画图卡通,windows画图工具怎么画卡通头像?
  11. 如何彻底卸载office!!
  12. 算法笔记学习day1(第二章)
  13. spring扫描出现Annotation-specified bean name 'userService' for bean class [com.test.service.UserService]
  14. 魔兽地图服务器修改,魔兽争霸3冰封王座地图编辑器修改无限人口的方法
  15. arcgis api for javascript 的swipe的使用
  16. WIN10任务栏卡死,鼠标一直转圈(亲测有效)
  17. Bitmap 图片缩放
  18. Design Compiler工具学习笔记(7)
  19. SecureCRT 注册机使用方法
  20. MySQL连接速度太慢_mysql-连接速度非常慢(1秒)

热门文章

  1. 快速失败Vs安全失败(Java迭代器附示例)
  2. Java并发编程(2):线程中断(含代码)
  3. 用JavaScript玩转计算机图形学(一)光线追踪入门
  4. 学生如何提高专业英文阅读能力--施一公教授
  5. Windows XP Ghost系统安装
  6. 编程之美-求数组中最长递增子序列(LIS)方法整理
  7. JavaScript 设计模式之观察者模式与发布订阅模式
  8. 提高 TDD 效率的一些小诀窍
  9. 网易云轻舟微服务深度解读:基于开源,强于开源
  10. C#开发微信门户及应用(44)--微信H5页面开发的经验总结