springboot关于@RabbitListener以及@QueueBinding的路由键设置没效果?

我使用rabbitmq和springboot集成出现一个坑。
那就是我发送消息时使用的是同一个消息队列以及同一个直连交换器,通过不同的key发给不同的消息监听器。
结果key-a的消息可以被key-b接受,我很奇怪。是springboot的bug吗?还是我的使用方式有问题。
springboot-amqp使用的2.1.3版本的

<groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-amqp</artifactId>
  <version>2.1.3.RELEASE</version>

a类:

@RabbitListener(
        bindings = {
                @QueueBinding(
                        value = @Queue(value = "QUEUE_All"),
                        exchange = @Exchange("itc-mq-exchange_All"),
                        key = {
                                "key_A"
                        }
                )
        }
)
public class ReceiverKeyA {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    //由于rabbitTemplate的scope属性设置为ConfigurableBeanFactory.SCOPE_PROTOTYPE,所以不能自动注入
    private RabbitTemplate rabbitTemplate;
    /**
     * 构造方法注入rabbitTemplate
     */
    @Autowired
    public ReceiverKeyA(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
//        rabbitTemplate.setConfirmCallback(this); //rabbitTemplate如果为单例的话,那回调就是最后设置的内容

    }

    @RabbitHandler
    public void process(String hello) {
        System.out.println("Receiver 接受spring-boot-routingKey_A队列 : " + hello);
    }


}

b类:

@RabbitListener(
        bindings = {
                @QueueBinding(
                        value = @Queue(value = "QUEUE_All"),
                        exchange = @Exchange("itc-mq-exchange_All"),
                        key = {
                                "key_B",
                        }
                )
        }
)
public class ReceiverKeyB {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    //由于rabbitTemplate的scope属性设置为ConfigurableBeanFactory.SCOPE_PROTOTYPE,所以不能自动注入
    private RabbitTemplate rabbitTemplate;
    /**
     * 构造方法注入rabbitTemplate
     */
    @Autowired
    public ReceiverKeyB(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
//        rabbitTemplate.setConfirmCallback(this); //rabbitTemplate如果为单例的话,那回调就是最后设置的内容

    }

    @RabbitHandler
    public void process(String hello) {
        System.out.println("Receiver 接受spring-boot-routingKey_B队列 : " + hello);
    }


}

结果是

Receiver 接受spring-boot-routingKey_A队列 : sdfaf 123sadfqwer asdf 东方购物
Receiver 接受spring-boot-routingKey_B队列 : sdfaf 123sadfqwer asdf 东方购物
Receiver 接受spring-boot-routingKey_A队列 : sdfaf 123sadfqwer asdf 东方购物
Receiver 接受spring-boot-routingKey_B队列 : sdfaf 123sadfqwer asdf 东方购物

太奇怪了,这个问题。。。

希望大家帮忙解决这个问题

阅读 4.9k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题