当前位置:   article > 正文

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

rabbittemplate.convertandsend同步消息

上一篇文章通过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);

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/399264
推荐阅读
相关标签
  

闽ICP备14008679号