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

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

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

1.注解配置AnnotationConfiguration.Java

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

2.消费者代码Consumer.java

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

3.生产者代码Producer.java

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

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

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

[java] view plain copy
 
 print?
  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);  
原文地址:https://www.cnblogs.com/telwanggs/p/7124752.html