(转)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 ...
- 【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 ...
随机推荐
- Ubuntu安装RTX2080显卡驱动
安装RTX2080显卡驱动 近日新购了一台DELL服务器,用于TensorFlow,由于显卡是另加的,需要安装显卡驱动. 服务器配置 服务器型号:DELL PowerEdge R730 CPU:2*I ...
- C++ 对象创建的问题
一.C++对象的创建: 对象创建的注意事项: 1.对象数组里的个数,就是创建对象的个数,普通数组一样:下标从0 到数组里数字 -1: 2.类名* 对象指针 <--> 这里只是一个指 ...
- Java 实现线程安全的三种方式
一个程序在运行起来的时候会转换成进程,通常含有多个线程. 通常情况下,一个进程中的比较耗时的操作(如长循环.文件上传下载.网络资源获取等),往往会采用多线程来解决. 比如显示生活中,银行取钱问题.火车 ...
- 【例题4-2 uva489】Hangman Judge
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 水题. 中间就赢了算赢.(重复说,算错 [代码] #include <bits/stdc++.h> using name ...
- Oracle关联查询-数据类型不一致问题 ORA-01722: 无效数字
一.存在表A和表B,都包含字段user_no,但数据类型不一致,如下: create table A ( user_id varchar2(20), user_no number(12,0), xxx ...
- 【ACM】hdu_zs3_1003_绝对值排序_201308100742
绝对值排序 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)Total Submissi ...
- ListViewItem中的图片不能动态改变的解决方法
近期遇到了一个问题,就是我的listviewitem中有个图片,点击的时候须要变成还有一种图片.结果在getView()中设置了响应.可是能够运行.就是不起作用.在网上查了非常多资料也没有解决.最后发 ...
- apple 团队电话
back 苹果电话:400 670 18552 这个是国内能打通的
- SpringMVC实战(三种映射处理器)
1.前言 上一篇博客,简单的介绍了一下SpringMVC的基础知识,这篇博客来说一下SpringMVC中的几种映射处理器机制. 2.三种映射处理器 2.1 BeanNameUrlHandlerMapp ...
- Ubuntu安装及ubuntu系统使用菜岛教程
Ubuntu是一款广受欢迎的开源Linux发行版,和其他Linux操作系统相比,Ubuntu非常易用,和Windows相容性很好,非常适合Windows用户的迁移,在其八年的成长过程中已经获得了两千多 ...