首页 > 解决方案 > Spring boot RabbitMQ Listener Creating Duplicate Queue

问题描述

I have Two separate Applications, one is for producer and one is the consumer with exchange type Default(DIRECT).

Below is the configuration for Rabbit MQ Producer with Dead letter queue settings as well.

    @Bean
    Queue dlq() {
        return QueueBuilder.durable(dlqQueueName).build();
    }

    @Bean
    Queue queue() {
        return QueueBuilder
                .durable(queueName)
                .withArgument("x-dead-letter-exchange", dlqExchange)
                .withArgument("x-dead-letter-routing-key", deadLetterRoutingKey)
                .withArgument("x-message-ttl",messageTTL)
                .build();
    }

    @Bean
    Binding binding() {
        return BindingBuilder.bind(queue()).to(exchange()).with(routingkey);
    }

    @Bean
    Binding dlqBinding() {
        return BindingBuilder.bind(dlq()).to(deadLetterExchange()).with(deadLetterRoutingKey);
    }

Now Here is the code for Rabbit MQ listener in a separate application.

  @Component
    public class RabbitMqConsumer implements MessageListener {
 
    
        @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "${queuename}", durable = "true"),
                exchange = @Exchange(value = "exchange"),key = "routingkey"))
        public void message(MyClass o) {
                //save to db
        }
  @Override
    public void onMessage(Message message) {
        // TODO Auto-generated method stub

    }

Now When I Run The consumer service, it creates a duplicate queue with same name. As shown in below picture

Rabbit MQ GUI

I also tried below setting in consumer but same result

@Bean
    public Queue queue() {
        return QueueBuilder
                .durable(env.getProperty("queue"))
                 .ttl(ttl)
                 .deadLetterExchange(ddlE)
                 .deadLetterRoutingKey(env.getProperty("dle.routingkey"))
                .build();
    }

Note: This issue started to happen when I added dead letter queue settings in the producer, before this setting, I had this(mentioned below) bean method in both configuration classes of consumer and producer and it was working fine.

 @Bean
        public Queue queue() {
            return new Queue(queueName,true);
        }

Any issues in above configuration? why its duplicating the queue, I could not figure it out just their features are different

Spring-boot-version : <version>2.5.2</version>

标签: rabbitmqspring-amqpspring-boot-2

解决方案


您还没有共享属性文件!

您必须在属性文件中写入队列名称的末尾有一些隐藏字符或空格。

匹配消费者和生产者属性文件中的两个名称。应用程序之一(消费者或生产者)正在创建带有空格或隐藏字符的队列作为队列名称的一部分!


推荐阅读