RabbitMQ学习(十二)之spring整合发送异步消息(注解实现)

来源:互联网 发布:北京网络职业学院 地图 编辑:程序博客网 时间:2024/05/16 17:39

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

1.生产者配置ProducerConfiguration.Java

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

[java] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. package cn.slimsmart.rabbitmq.demo.spring.async;  
  2.   
  3. import org.springframework.context.annotation.AnnotationConfigApplicationContext;  
  4.   
  5. public class Producer {  
  6.   
  7.     public static void main(String[] args) {  
  8.         new AnnotationConfigApplicationContext(ProducerConfiguration.class);  
  9.     }  
  10. }  
3.接收消息处理类ReceiveMsgHandler.java

[java] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. package cn.slimsmart.rabbitmq.demo.spring.async;  
  2.   
  3. public class ReceiveMsgHandler {  
  4.   
  5.     public void handleMessage(String text) {  
  6.         System.out.println("Received: " + text);  
  7.     }  
  8. }  
4.消费者配置ConsumerConfiguration

[java] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. package cn.slimsmart.rabbitmq.demo.spring.async;  
  2.   
  3. import org.springframework.amqp.core.AmqpAdmin;  
  4. import org.springframework.amqp.core.Queue;  
  5. import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;  
  6. import org.springframework.amqp.rabbit.connection.ConnectionFactory;  
  7. import org.springframework.amqp.rabbit.core.RabbitAdmin;  
  8. import org.springframework.amqp.rabbit.core.RabbitTemplate;  
  9. import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;  
  10. import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;  
  11. import org.springframework.context.annotation.Bean;  
  12. import org.springframework.context.annotation.Configuration;  
  13.   
  14. import com.rabbitmq.client.AMQP;  
  15.   

  16. public class ConsumerConfiguration {  
  17.   
  18.     // 指定队列名称 routingkey的名称默认为Queue的名称,使用Exchange类型为DirectExchange  
  19.     protected String springQueueDemo = "spring-queue-async";  
  20.   
  21.     // 创建链接  
  22.     @Bean  
  23.     public ConnectionFactory connectionFactory() {  
  24.         CachingConnectionFactory connectionFactory = new CachingConnectionFactory(  
  25.                 "192.168.36.102");  
  26.         connectionFactory.setUsername("admin");  
  27.         connectionFactory.setPassword("admin");  
  28.         connectionFactory.setPort(AMQP.PROTOCOL.PORT);  
  29.         return connectionFactory;  
  30.     }  
  31.   
  32.     // 创建rabbitAdmin 代理类  
  33.     @Bean  
  34.     public AmqpAdmin amqpAdmin() {  
  35.         return new RabbitAdmin(connectionFactory());  
  36.     }  
  37.   
  38.     // 创建rabbitTemplate 消息模板类  
  39.     @Bean  
  40.     public RabbitTemplate rabbitTemplate() {  
  41.         RabbitTemplate template = new RabbitTemplate(connectionFactory());  
  42.         // The routing key is set to the name of the queue by the broker for the  
  43.         // default exchange.  
  44.         template.setRoutingKey(this.springQueueDemo);  
  45.         // Where we will synchronously receive messages from  
  46.         template.setQueue(this.springQueueDemo);  
  47.         return template;  
  48.     }  
  49.   
  50.     //  
  51.     // Every queue is bound to the default direct exchange  
  52.     public Queue helloWorldQueue() {  
  53.         return new Queue(this.springQueueDemo);  
  54.     }  
  55.   
  56.     @Bean  
  57.     public SimpleMessageListenerContainer listenerContainer() {  
  58.         SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();  
  59.         container.setConnectionFactory(connectionFactory());  
  60.         container.setQueueNames(this.springQueueDemo);  
  61.         container.setMessageListener(new MessageListenerAdapter(  
  62.                 new ReceiveMsgHandler()));  
  63.         return container;  
  64.     }  
  65.   
  66. }  
5.消费者启动类Consumer.java

[java] view plain copy print?在CODE上查看代码片派生到我的代码片
  1. package cn.slimsmart.rabbitmq.demo.spring.async;  
  2.   
  3. import org.springframework.context.annotation.AnnotationConfigApplicationContext;  
  4.   
  5. public class Consumer {  
  6.     public static void main(String[] args) {  
  7.         new AnnotationConfigApplicationContext(ConsumerConfiguration.class);  
  8.     }  
  9. }  
启动接收消息,再发送消息

[sql] view plain copy print?在CODE上查看代码片派生到我的代码片
  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消息队列不存在,请在控制台添加。

0 0
原创粉丝点击