Cannot obtain exclusive access to locked queue

jandres picture jandres · Apr 14, 2016 · Viewed 12.5k times · Source

I have an anonymous and exclusive queue defined like this:

@Bean 
    public SimpleMessageListenerContainer responseMessageListenerContainer(){
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(simpleRoutingConnectionFactory());
        container.setQueues(responseAnonymousQueue());
        container.setMessageListener(rabbitTemplate());
        container.setAcknowledgeMode(AcknowledgeMode.AUTO);
        container.setMessageConverter(jsonMessageConverter());
        return container;
    }

@Bean
    public Queue responseAnonymousQueue() { 
        return new MyAnonymousQueue();
    }

Sometimes I get this error en rabbitmq log:

=ERROR REPORT==== 12-Apr-2016::15:13:42 === Channel error on connection <0.6899.0> (XX.XXX.57.174:51716 -> 192.168.100.145:5671, vhost: '/', user: 'XXXX_USER'), channel 1: {amqp_error,resource_locked, "cannot obtain exclusive access to locked queue ' XXXX_USER-broad-1457bb43-6487-4252-b21a-a5a92d19e0dc' in vhost '/'", 'queue.declare'}

So the client can’t declare the queue and it can’t receive the messages from the AMQP server.

It happens after this message:

=WARNING REPORT==== 12-Apr-2016::15:11:51 === closing AMQP connection <0.6810.0> (XX.XXX.57.174:17959 -> 192.168.100.145:5671): connection_closed_abruptly

=INFO REPORT==== 12-Apr-2016::15:13:41 === accepting AMQP connection <0.6899.0> (XX.XXX.57.174:51716 -> 192.168.100.145:5671)

I can’t reproduce it (I have tried closing the connection from rabbitmq and removing the network cable, but the application reconnect well again), so I don’t know exactly why is this happening. It is supposed that private and exclusive queues are deleted with the closing of the connection, so why is this happening? How can I catch this exception and recover from it?

Thanks

Answer

Gary Russell picture Gary Russell · Apr 14, 2016

You are correct, exclusive queues are deleted when the connection that declared it; this implies that that connection is still open and it wasn't declared by the connection you see in the log.

When your system is in that condition, go to the admin UI where you can explore the queue and which connection owns it.

e.g. Exclusive owner 127.0.0.1:60113

If that shows the closed connection (XX.XXX.57.174:17959 in the example above) you should reach out to the rabbitmq guys on the rabbitmq-users google group, this does not appear to be a spring-amqp issue;

EDIT

FYI, if passive queue declaration fails for any reason, by default the consumer will try 3 times at 5 second intervals, then give up and stop the container.

There are two properties on the container that can be used to adjust this - declarationRetries and failedDeclarationRetryInterval (default 3 and 5000 respectively). If you are using <rabbit:listener-container /> configuration, there are equivalent attributes on the namespace.