(转) RabbitMQ学习之spring整合发送同步消息(注解实现)
http://blog.csdn.net/zhu_tianwei/article/details/40918477
上一篇文章通过xml配置rabbitmq的rabbitTemplate,本节将使用注解的形式实现同步消息的发送。
1.注解配置AnnotationConfiguration.Java
- package cn.slimsmart.rabbitmq.demo.spring.sync;
- 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.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import com.rabbitmq.client.AMQP;
- @Configuration
- public class AnnotationConfiguration {
- //指定队列名称 routingkey的名称默认为Queue的名称,使用Exchange类型为DirectExchange
- protected String springQueueDemo = "spring-queue-demo";
- //创建链接
- @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 Binding binding() {
- return declare(new Binding(helloWorldQueue(), defaultDirectExchange()));
- }*/
- /*
- @Bean
- public TopicExchange helloExchange() {
- return declare(new TopicExchange("hello.world.exchange"));
- }*/
- /*
- public Queue declareUniqueQueue(String namePrefix) {
- Queue queue = new Queue(namePrefix + "-" + UUID.randomUUID());
- rabbitAdminTemplate().declareQueue(queue);
- return queue;
- }
- // if the default exchange isn't configured to your liking....
- @Bean Binding declareP2PBinding(Queue queue, DirectExchange exchange) {
- return declare(new Binding(queue, exchange, queue.getName()));
- }
- @Bean Binding declarePubSubBinding(String queuePrefix, FanoutExchange exchange) {
- return declare(new Binding(declareUniqueQueue(queuePrefix), exchange));
- }
- @Bean Binding declarePubSubBinding(UniqueQueue uniqueQueue, TopicExchange exchange) {
- return declare(new Binding(uniqueQueue, exchange));
- }
- @Bean Binding declarePubSubBinding(String queuePrefix, TopicExchange exchange, String routingKey) {
- return declare(new Binding(declareUniqueQueue(queuePrefix), exchange, routingKey));
- }*/
- }
2.消费者代码Consumer.java
- package cn.slimsmart.rabbitmq.demo.spring.sync;
- import org.springframework.amqp.core.AmqpTemplate;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
- public class Consumer {
- public static void main(String[] args) {
- ApplicationContext context = new AnnotationConfigApplicationContext(AnnotationConfiguration.class);
- AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);
- System.out.println("Received: " + amqpTemplate.receiveAndConvert());
- }
- }
3.生产者代码Producer.java
- package cn.slimsmart.rabbitmq.demo.spring.sync;
- import org.springframework.amqp.core.AmqpTemplate;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
- public class Producer {
- public static void main(String[] args) {
- ApplicationContext context = new AnnotationConfigApplicationContext(AnnotationConfiguration.class);
- AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);
- amqpTemplate.convertAndSend("Hello World");
- System.out.println("Sent: Hello World");
- }
- }
运行生产者向队列中发送一条消息,再运行消费者消费消息。
另外,声明一个队列代码如:
- ApplicationContext context = new AnnotationConfigApplicationContext(AnnotationConfiguration.class);
- AmqpAdmin amqpAdmin = context.getBean(AmqpAdmin.class);
- Queue helloWorldQueue = new Queue("create.world.queue");
- amqpAdmin.declareQueue(helloWorldQueue);
(转) RabbitMQ学习之spring整合发送同步消息(注解实现)的更多相关文章
- (转)RabbitMQ学习之spring整合发送同步消息
http://blog.csdn.net/zhu_tianwei/article/details/40890543 以下实现使用Exchange类型为DirectExchange. routingke ...
- (转)RabbitMQ学习之spring整合发送异步消息(注解实现)
http://blog.csdn.net/zhu_tianwei/article/details/40919249 实现使用Exchange类型为DirectExchange. routingkey的 ...
- (转) RabbitMQ学习之spring整合发送异步消息
http://blog.csdn.net/zhu_tianwei/article/details/40919031 实现使用Exchange类型为DirectExchange. routingkey的 ...
- 【RocketMQ源码学习】- 3. Client 发送同步消息
本文较长,代码后面给了方法简图,希望给你帮助 发送的方式 同步发送 异步发送 消息的类型 普通消息 顺序消息 事务消息 发送同步消息的时序图 为了防止读者朋友嫌烦,可以看下时序图,后面我也会给出方法的 ...
- ActiveMQ学习总结------Spring整合ActiveMQ 04
通过前几篇的学习,相信大家已经对我们的ActiveMQ的原生操作已经有了个深刻的概念, 那么这篇文章就来带领大家一步一步学习下ActiveMQ结合Spring的实战操作 注:本文将省略一部分与Acti ...
- RabbitMQ学习笔记之五种模式及消息确认机制
本文详细介绍简单模式Simple.工作模式Work.发布订阅模式Publish/Subscribe.Topic.Routing. Maven依赖引用 <dependencies> < ...
- Spring整合ActiveMQ实现消息延迟投递和定时投递
linux(centos)系统安装activemq参考:https://www.cnblogs.com/pxblog/p/12222231.html 首先在ActiveMQ的安装路径 /conf/ac ...
- RabbitMQ走过的坑,发送的消息是乱码
发送的消息在可视化界面中是乱码,如图: 看见这个content_tpye没有,是不是很奇怪,就是这个坑,设置下就行,看代码: @Bean Jackson2JsonMessageConverter me ...
- RabbitMQ学习之spring配置文件rabbit标签的使用
下面我们通过一个实例看一下rabbit的使用. 1.实现一个消息监听器ReceiveMessageListener.Java package org.springframework.amqp.core ...
随机推荐
- 基于 Nginx XSendfile + SpringMVC 进行文件下载
转自:http://denger.iteye.com/blog/1014066 基于 Nginx XSendfile + SpringMVC 进行文件下载 PS:经过实际测试,通过 nginx 提供文 ...
- 填坑...P1546 最短网络 Agri-Net
P1546 最短网络 Agri-Net 难度普及/提高- 时空限制1s / 128MB 题目背景 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要 ...
- 今天写了一个简单的新浪新闻RSS操作类库
今天,有位群友问我如何获新浪新闻列表相关问题,我想,用正则表达式网页中取显然既复杂又不一定准确,现在许多大型网站都有RSS集合,所以我就跟他说用RSS应该好办一些. 一年前我写过一个RSS阅读器,不过 ...
- Bootstrap内联表单
有时候我们需要将表单的控件都在一行内显示,就需要将表单控件设置成内联块元素(display:inline-block). 在Bootstrap框架中实现这样的表单效果是轻而易举的,你只需要在<f ...
- Java基础学习总结(67)——Java接口API中使用数组的缺陷
如果你发现在一个接口使用有如下定义方法: public String[] getParameters(); 那么你应该认真反思.数组不仅仅老式,而且我们有合理的理由避免暴露它们.在这篇文章中,我将试图 ...
- (32)Spring Boot使用@SpringBootApplication注解,从零开始学Spring Boot
[来也匆匆,去也匆匆,在此留下您的脚印吧,转发点赞评论] 如果看了我之前的文章,这个节你就可以忽略了,这个是针对一些刚入门的选手存在的困惑进行写的一篇文章. 很多Spring Boot开发者总是使用 ...
- Spring Boot-整合Mybatis(五)
原始的整合方式 1.添加依赖 <!--mybatis起步依赖--> <dependency> <groupId>org.mybatis.spring.boot< ...
- ASP的Global.asa使用说明
/*-------------------ASP文档参考集-----------------------*/ *-->作者:草履虫 *-->时间:2007-4.28---2007-4.30 ...
- Linux查看文件内容命令:more(转)
Linux more命令类似cat ,不过会以一页一页的形式显示,更方便使用者逐页阅读,而最基本的指令就是按空白键(space)就往下一页显示,按b键就会往回(back)一页显示,而且还有搜寻字串的功 ...
- Python:利用 selenium 库抓取动态网页示例
前言 在抓取常规的静态网页时,我们直接请求对应的 url 就可以获取到完整的 HTML 页面,但是对于动态页面,网页显示的内容往往是通过 ajax 动态去生成的,所以如果是用 urllib.reque ...