Search code examples
rabbitmqspring-amqpspring-boot-2

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>


Solution

  • You haven't shared properties file!

    You must have some hidden characters or spaces at the end of the queue name written in property file.

    Match both names in properties file of consumer and producer. One of the application (consumer or producer), is creating queue with space or hidden characters as a part of queue name!