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

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

1.生产者配置ProducerConfiguration.Java

  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

  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

  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

  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

  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. }

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

  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. 【RocketMQ源码学习】- 3. Client 发送同步消息

    本文较长,代码后面给了方法简图,希望给你帮助 发送的方式 同步发送 异步发送 消息的类型 普通消息 顺序消息 事务消息 发送同步消息的时序图 为了防止读者朋友嫌烦,可以看下时序图,后面我也会给出方法的 ...

  5. ActiveMQ学习总结------Spring整合ActiveMQ 04

    通过前几篇的学习,相信大家已经对我们的ActiveMQ的原生操作已经有了个深刻的概念, 那么这篇文章就来带领大家一步一步学习下ActiveMQ结合Spring的实战操作 注:本文将省略一部分与Acti ...

  6. RabbitMQ学习笔记之五种模式及消息确认机制

    本文详细介绍简单模式Simple.工作模式Work.发布订阅模式Publish/Subscribe.Topic.Routing. Maven依赖引用 <dependencies> < ...

  7. Spring整合ActiveMQ实现消息延迟投递和定时投递

    linux(centos)系统安装activemq参考:https://www.cnblogs.com/pxblog/p/12222231.html 首先在ActiveMQ的安装路径 /conf/ac ...

  8. RabbitMQ走过的坑,发送的消息是乱码

    发送的消息在可视化界面中是乱码,如图: 看见这个content_tpye没有,是不是很奇怪,就是这个坑,设置下就行,看代码: @Bean Jackson2JsonMessageConverter me ...

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

    下面我们通过一个实例看一下rabbit的使用. 1.实现一个消息监听器ReceiveMessageListener.Java package org.springframework.amqp.core ...

随机推荐

  1. 单个图片获取-爬取网易"数读"信息数据(暴涨的房租,正在摧毁中国年轻人的生活)

    参考链接:https://www.makcyun.top/web_scraping_withpython3.html 该网页其实有16张图片,但是因为页面数据中某处多个空白,导致参考链接中的方式只有1 ...

  2. 实体服务器安装centos7过程记录

    一次在实体服务器安装centos 7的过程记录 第一次在实体服务器上面安装服务器(centos 7),在此记录安装过程中遇到的一些坑. 系统版本:CentOS Linux release 7.6.18 ...

  3. jQuery cxCalendar 日期选择器

    简介 cxCalendar 是基于 jQuery 的日期选择器插件. 它灵活自由,你可以自定义外观,日期的范围,返回的格式等. 版本: jQuery v1.7+ jQuery cxCalendar v ...

  4. node源码详解(五)

    本作品采用知识共享署名 4.0 国际许可协议进行许可.转载保留声明头部与原文链接https://luzeshu.com/blog/nodesource5 本博客同步在https://cnodejs.o ...

  5. BZOJ 4244 邮戳拉力赛 (DP)

    手动博客搬家: 本文发表于20181211 18:01:21, 原地址https://blog.csdn.net/suncongbo/article/details/84957907 为了防止我的博客 ...

  6. d正则表达式相关

    控件看Delphi正则表达式使用方法(TPerlRegEx)的介绍: 使用正则表达式获取Sql查询语句各项(表名.字段.条件.排序) 希望在线测试正则表达式  在线正则表达式测试 有许多常规表达式 希 ...

  7. 数位dp无前导零

    题目链接:http://    www.lydsy.com/JudgeOnline/problem.php?id=1026 #include <iostream> #include < ...

  8. caffe Solve函数

    下面来看Solver<Dtype>::Solve(const char* resume_file) solver.cpp template <typename Dtype> v ...

  9. 《随笔》pyqt 获取 TreeWidget 选中项的内容

    感谢朋友支持本博客,欢迎共同探讨交流.因为能力和时间有限,错误之处在所难免.欢迎指正! 假设转载,请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...

  10. cocos2d函数

    CCNodeLoader::parseProperties CCBReader::readNodeGraph kCCBPropTypeBlockCCControl CCInvocation parse ...