当前位置:   article > 正文

SpringAMQP创建交换机和队列_java 使用spring qmpa 创建队列和交换机

java 使用spring qmpa 创建队列和交换机

SpringAMQP提供的Exchange接口        

一基于bean注解:

一.Fanout交换机
  1. package com.itheima.consumer.config;
  2. import org.springframework.amqp.core.Binding;
  3. import org.springframework.amqp.core.BindingBuilder;
  4. import org.springframework.amqp.core.FanoutExchange;
  5. import org.springframework.amqp.core.Queue;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. @Configuration
  9. public class FanoutConfiguration {
  10. //生成一个fanoutExchange交互机(hmall,fanout2)
  11. @Bean
  12. public FanoutExchange fanoutExchange(){
  13. // ExchangeBuilder.fanoutExchange("").build();
  14. return new FanoutExchange("hmall.fanout2");
  15. }
  16. //生成一个新的队列fanout.queue3
  17. @Bean
  18. public Queue fanoutQueue3(){
  19. // QueueBuilder.durable("ff").build();
  20. return new Queue("fanout.queue3");
  21. }
  22. //将队列fanoutQueue3绑定到fanoutExchange交互机
  23. @Bean
  24. public Binding fanoutBinding3(Queue fanoutQueue3, FanoutExchange fanoutExchange){
  25. return BindingBuilder.bind(fanoutQueue3).to(fanoutExchange);
  26. }
  27. //生成一个新的队列fanout.queue4
  28. @Bean
  29. public Queue fanoutQueue4(){
  30. return new Queue("fanout.queue4");
  31. }
  32. //将队列fanoutQueue4绑定到fanoutExchange交互机
  33. @Bean
  34. public Binding fanoutBinding4(){
  35. return BindingBuilder.bind(fanoutQueue4()).to(fanoutExchange());
  36. }
  37. }

二.Direct交换机

同理于Fanout交换机

  1. package com.itheima.consumer.config;
  2. import org.springframework.amqp.core.Binding;
  3. import org.springframework.amqp.core.BindingBuilder;
  4. import org.springframework.amqp.core.DirectExchange;
  5. import org.springframework.amqp.core.Queue;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. @Configuration
  9. public class DirectConfiguration {
  10. @Bean
  11. public DirectExchange directExchange(){
  12. return new DirectExchange("hmall.direct");
  13. }
  14. @Bean
  15. public Queue directQueue1(){
  16. return new Queue("direct.queue1");
  17. }
  18. @Bean
  19. public Binding directQueue1BindingRed(Queue directQueue1, DirectExchange directExchange){
  20. return BindingBuilder.bind(directQueue1).to(directExchange).with("red");
  21. }
  22. @Bean
  23. public Binding directQueue1BindingBlue(Queue directQueue1, DirectExchange directExchange){
  24. return BindingBuilder.bind(directQueue1).to(directExchange).with("blue");
  25. }
  26. @Bean
  27. public Queue directQueue2(){
  28. return new Queue("direct.queue2");
  29. }
  30. @Bean
  31. public Binding directQueue2BindingRed(Queue directQueue2, DirectExchange directExchange){
  32. return BindingBuilder.bind(directQueue2).to(directExchange).with("red");
  33. }
  34. @Bean
  35. public Binding directQueue2BindingBlue(Queue directQueue2, DirectExchange directExchange){
  36. return BindingBuilder.bind(directQueue2).to(directExchange).with("yellow");
  37. }
  38. }

二.基于@RabbitListener注解

  1. @RabbitListener(bindings = @QueueBinding(
  2. value = @Queue(name = "direct.queue1", durable = "true"),
  3. exchange = @Exchange(name = "hmall.direct", type = ExchangeTypes.DIRECT),
  4. key = {"red", "blue"}
  5. ))
  6. public void listenDirectQueue1(String msg) throws InterruptedException {
  7. System.out.println("消费者1 收到了 direct.queue1的消息:【" + msg +"】");
  8. }
  9. @RabbitListener(bindings = @QueueBinding(
  10. value = @Queue(name = "direct.queue2", durable = "true"),
  11. exchange = @Exchange(name = "hmall.direct", type = ExchangeTypes.DIRECT),
  12. key = {"red", "yellow"}
  13. ))
  14. public void listenDirectQueue2(String msg) throws InterruptedException {
  15. System.out.println("消费者2 收到了 direct.queue2的消息:【" + msg +"】");
  16. }

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

闽ICP备14008679号