Search code examples
rabbitmqspring-rabbit

First message to RabbitMQ queue causes channel shutdown


The first message to my queue always fails. From the second one, everything works just fine!

Stacktrace

Not sure if that's readable so :

Created new connection: rabbitConnectionFactory#1b940034:0/SimpleConnection@2c52fbff [delegate=amqp://guest@10.0.0.10:5672/, localPort= 36370]

Channel shutdown: channel error; protocol method: #method<channel.close>(reply-code=406, reply-text=PRECONDITION_FAILED - inequivalent arg 'auto_delete' for exchange 'rabbitmq_exchange' in vhost '/': received 'false' but current is 'true', class-id=40, method-id=10)

I'm not sure why this is happening, because I launch this on a fresh VM (AWS EC2 instance) every single time. How could "current be true"?

I suppose something is badly configured in the Spring Boot publisher:

enter image description here

Not sure if that's readable so :

@Configuration
public class RabbitMqConfig {
    @Bean
    Queue queue() {
        return new Queue(System.getenv("RABBITMQ_QUEUE_NAME"), true,false, false);
    }

    @Bean
    DirectExchange exchange() {
        return new DirectExchange(System.getenv("RABBITMQ_EXCHANGE_NAME"), true, false);
    }

    @Bean
    Binding binding(Queue queue, DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(System.getenv("RABBITMQ_ROUTING_KEY"));
    }

    @Bean
    public MessageConverter jsonMessageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    public AmqpTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(jsonMessageConverter());
        return rabbitTemplate;
    }
}

So what's going wrong here? Thanks!


Solution

  • The error is quite clear...

    Channel shutdown: channel error; protocol method: #method(reply-code=406, reply-text=PRECONDITION_FAILED - inequivalent arg 'auto_delete' for exchange 'rabbitmq_exchange' in vhost '/': received 'false' but current is 'true', class-id=40, method-id=10)

    When the connection is first opened, the framework looks for queues/bindings etc and declares them. If a queue exists already, it must have the same properties/arguments.

    @Bean
    Queue queue() {
        return new Queue(System.getenv("RABBITMQ_QUEUE_NAME"), true, false, false);
    }
    

    Presumably, on the consumer side, it is being declared with different properties...

    @Bean
    Queue queue() {
        return new Queue(System.getenv("RABBITMQ_QUEUE_NAME"), true, false, true);
    }
    

    (the auto_delete is inequivalent).

    They must be the same.