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

上一篇文章通过xml配置rabbitmq的rabbitTemplate,本节将使用注解的形式实现同步消息的发送。

1.注解配置AnnotationConfiguration.Java

  1. package cn.slimsmart.rabbitmq.demo.spring.sync;
  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.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import com.rabbitmq.client.AMQP;
  11. @Configuration
  12. public class AnnotationConfiguration {
  13. //指定队列名称 routingkey的名称默认为Queue的名称,使用Exchange类型为DirectExchange
  14. protected String springQueueDemo = "spring-queue-demo";
  15. //创建链接
  16. @Bean
  17. public ConnectionFactory connectionFactory() {
  18. CachingConnectionFactory connectionFactory = new CachingConnectionFactory("192.168.36.102");
  19. connectionFactory.setUsername("admin");
  20. connectionFactory.setPassword("admin");
  21. connectionFactory.setPort(AMQP.PROTOCOL.PORT);
  22. return connectionFactory;
  23. }
  24. //创建rabbitAdmin 代理类
  25. @Bean
  26. public AmqpAdmin amqpAdmin() {
  27. return new RabbitAdmin(connectionFactory());
  28. }
  29. //创建rabbitTemplate 消息模板类
  30. @Bean
  31. public RabbitTemplate rabbitTemplate() {
  32. RabbitTemplate template = new RabbitTemplate(connectionFactory());
  33. //The routing key is set to the name of the queue by the broker for the default exchange.
  34. template.setRoutingKey(this.springQueueDemo);
  35. //Where we will synchronously receive messages from
  36. template.setQueue(this.springQueueDemo);
  37. return template;
  38. }
  39. //
  40. // Every queue is bound to the default direct exchange
  41. public Queue helloWorldQueue() {
  42. return new Queue(this.springQueueDemo);
  43. }
  44. /*
  45. @Bean
  46. public Binding binding() {
  47. return declare(new Binding(helloWorldQueue(), defaultDirectExchange()));
  48. }*/
  49. /*
  50. @Bean
  51. public TopicExchange helloExchange() {
  52. return declare(new TopicExchange("hello.world.exchange"));
  53. }*/
  54. /*
  55. public Queue declareUniqueQueue(String namePrefix) {
  56. Queue queue = new Queue(namePrefix + "-" + UUID.randomUUID());
  57. rabbitAdminTemplate().declareQueue(queue);
  58. return queue;
  59. }
  60. // if the default exchange isn't configured to your liking....
  61. @Bean Binding declareP2PBinding(Queue queue, DirectExchange exchange) {
  62. return declare(new Binding(queue, exchange, queue.getName()));
  63. }
  64. @Bean Binding declarePubSubBinding(String queuePrefix, FanoutExchange exchange) {
  65. return declare(new Binding(declareUniqueQueue(queuePrefix), exchange));
  66. }
  67. @Bean Binding declarePubSubBinding(UniqueQueue uniqueQueue, TopicExchange exchange) {
  68. return declare(new Binding(uniqueQueue, exchange));
  69. }
  70. @Bean Binding declarePubSubBinding(String queuePrefix, TopicExchange exchange, String routingKey) {
  71. return declare(new Binding(declareUniqueQueue(queuePrefix), exchange, routingKey));
  72. }*/
  73. }

2.消费者代码Consumer.java

  1. package cn.slimsmart.rabbitmq.demo.spring.sync;
  2. import org.springframework.amqp.core.AmqpTemplate;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  5. public class Consumer {
  6. public static void main(String[] args) {
  7. ApplicationContext context = new AnnotationConfigApplicationContext(AnnotationConfiguration.class);
  8. AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);
  9. System.out.println("Received: " + amqpTemplate.receiveAndConvert());
  10. }
  11. }

3.生产者代码Producer.java

  1. package cn.slimsmart.rabbitmq.demo.spring.sync;
  2. import org.springframework.amqp.core.AmqpTemplate;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  5. public class Producer {
  6. public static void main(String[] args) {
  7. ApplicationContext context = new AnnotationConfigApplicationContext(AnnotationConfiguration.class);
  8. AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);
  9. amqpTemplate.convertAndSend("Hello World");
  10. System.out.println("Sent: Hello World");
  11. }
  12. }

运行生产者向队列中发送一条消息,再运行消费者消费消息。

另外,声明一个队列代码如:

  1. ApplicationContext context =  new AnnotationConfigApplicationContext(AnnotationConfiguration.class);
  2. AmqpAdmin amqpAdmin = context.getBean(AmqpAdmin.class);
  3. Queue helloWorldQueue = new Queue("create.world.queue");
  4. amqpAdmin.declareQueue(helloWorldQueue);

(转) RabbitMQ学习之spring整合发送同步消息(注解实现)的更多相关文章

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

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

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

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

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

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

  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. Oracle开发常用函数 max 最大数 自动加 1

    max 最大数 自动加 1 create or replace function fun_getmaxlot( vend in varchar2 , domain IN VARCHAR2, tag i ...

  2. Socket编程(day14)

    一.基于TCP传输层的编程模型 TCP是面向连接的,安全可靠的. 三次握手 服务器端编程模型 .创建一个用于网络通讯的设备 通讯端点 socket() #include <sys/types.h ...

  3. python 实现kmeans聚类

    编程中在做数值相等判断的时候,直接使用==判断并不可靠.实际上经过运算后的两个值(浮点型)并不可能完全一致,可能会因为小数点后的些许差异导致判断为false. 比如: 1 print 1e-5 == ...

  4. vscode简单使用介绍及个人常用扩展插件

    vscode全称Visual Studio Code 是微软开发一款IDE,官方地址 vscode  作为一款前端编辑器功能很强大,灵活,可以根据个人喜好选择扩展插件,而且还支持多种开发语言, 关于v ...

  5. 洛谷 P1595 信封问题

    题目描述 某人写了n封信和n个信封,如果所有的信都装错了信封.求所有信都装错信封共有多少种不同情况. 输入输出格式 输入格式: 一个信封数n 输出格式: 一个整数,代表有多少种情况. 输入输出样例 输 ...

  6. codevs——T2894 Txx考试

    http://codevs.cn/problem/2894/  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Descri ...

  7. UVA The Tower of Babylon

    The Tower of Babylon Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many det ...

  8. 20160223.CCPP体系具体解释(0033天)

    程序片段(01):MyArray.h+MyArray.c+main.c 内容概要:数组库 ///MyArray.h #pragma once #define DT int//类型通用 typedef ...

  9. Apache vs. Nginx

    精简版 Apache:出名比较早,09年左右是最流行的时期,功能强大,可以根据需求配置为基于进程,基于线程或者基于事件的,但是消耗内存较多,对硬件需求较高,内存是影响服务器性能的最关键因素,在VPS上 ...

  10. HDU 3007

    基本小圆覆盖模板题 #include <iostream> #include <algorithm> #include <cmath> using namespac ...