rabbitmq ReturnCallback stuck while declaring queue on NO_ROUTE(312)
<p>I am trying to use the publisher returns callback to declare queue and bindings if no route is available so that messages aren't dropped again.
This is because my queue is auto-delete and would be deleted if my consumer goes down.</p>

<p>But the  ReturnCallback thread gets stuck in returnedMessage() at admin.declareQueue(queue). </p>

<p>On Further debugging I see that it is stuck in RabbitAdmin.declareQueue() at:
    <strong><em>DeclareOk[] declared = declareQueues(channel, queue);</em></strong></p>

<p>Though this call is stuck, I see the queue declared (checked via console). Also subsequent <em>send</em> calls do not invoke returnedMessage, since probably the first <em>returnedMessage</em> call hasn't returned yet.</p>

<p>I am doing something wrong here? Is it right to declare queue/bindings in return callback?</p>

<p>Any help would be really appreciated. Thanks.</p>

<p>Below is my ReturnCallback:</p>

<pre><code>public class MyReturnCallback implements ReturnCallback {
    // constructor, member initialization goes here

    @Override
    public void returnedMessage(Message message, int replyCode,
            String replyText, String exchangeName, String routingKey) {
        if (replyCode == 312) {
            if (this.exchangeName.equals(exchangeName) &amp;&amp; this.routingKey.equals(routingKey)) {
                RabbitAdmin admin = new RabbitAdmin(connectionFactory);
                Exchange exchange = new DirectExchange(exchangeName, true, false);
                Queue queue = new Queue(queueName, true, false, true);
                admin.declareQueue(queue);
                Binding binding = BindingBuilder.bind(queue).to((DirectExchange)exchange).with(routingKey); 
                admin.declareBinding(binding);
                if (null != binding) {
                    RabbitTemplate rabbitmqTemplate = new RabbitTemplate(connectionFactory);
                    logger.debug("Sending to [exchange:" + exchange.getName() + ", routing-key:" + routingKey + "]:" + message.toString());
                    rabbitmqTemplate.send(exchangeName, routingKey, message);
                }
            }
        }
    }
}
</code></pre>

<p>And my test producer is something like:</p>

<pre><code>public class TestProducer {
    // constructor, member initialization goes here

    void initialize()
        rabbitAdmin = new RabbitAdmin(connectionFactory);
        rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setExchange(exchangeName);
        rabbitTemplate.setMessageConverter(messageConverter);
        rabbitTemplate.setRoutingKey(routingKey);
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setReturnCallback(new RabbitReturnCallback());

        Exchange exchange = new DirectExchange(exchangeName, true, false);
        rabbitAdmin.declareExchange(exchange);
        Queue queue = new Queue(queueName, true, false, true);
        rabbitAdmin.declareQueue(queue);
        Binding binding = BindingBuilder.bind(queue).to((DirectExchange)exchange).with(routingKey); 
        rabbitAdmin.declareBinding(binding);
    }

    void send() {
        rabbitTemplate.convertAndSend(message);
    }
}
</code></pre>

<p>ConnectionFactory bean is:</p>

<pre><code>&lt;rabbit:connection-factory id="rabbitmqConnectionFactory"
    host="${rabbitmq.host:localhost}"
    port="${rabbitmq.port:5672}"
    username="${rabbitmq.username}"
    password="${rabbitmq.password}"
    virtual-host="${rabbitmq.vhost:/}"
    cache-mode="CHANNEL"
    channel-cache-size="${rabbitmq.channel-cache-size:25}" 
    publisher-returns="true"/&gt;
</code></pre>

<p>Attaching debug logs for reference:</p>

<pre><code>2016-10-04 17:58:58,881 [main] INFO  CachingConnectionFactory:291 - Created new connection: SimpleConnection@4f2b503c [delegate=amqp://test@ip-address:5672/]
2016-10-04 17:58:58,883 [main] DEBUG RabbitAdmin:399 - Initializing declarations
2016-10-04 17:58:58,883 [main] DEBUG DefaultListableBeanFactory:250 - Returning cached instance of singleton bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry'
2016-10-04 17:58:58,935 [main] DEBUG CachingConnectionFactory:453 - Creating cached Rabbit Channel from PublisherCallbackChannelImpl: AMQChannel(amqp://test@ip-address:5672/,1)
2016-10-04 17:58:58,975 [main] DEBUG PublisherCallbackChannelImpl:694 - Added listener org.springframework.amqp.rabbit.core.RabbitTemplate@67f639d3
2016-10-04 17:58:58,976 [main] DEBUG RabbitTemplate:1451 - Added pending confirms for Cached Rabbit Channel: PublisherCallbackChannelImpl: AMQChannel(amqp://test@ip-address:5672/,1), conn: Proxy@bae7dc0 Shared Rabbit Connection: SimpleConnection@4f2b503c [delegate=amqp://test@ip-address:5672/] to map, size now 1
2016-10-04 17:58:58,976 [main] DEBUG RabbitTemplate:1296 - Executing callback on RabbitMQ Channel: Cached Rabbit Channel: PublisherCallbackChannelImpl: AMQChannel(amqp://test@ip-address:5672/,1), conn: Proxy@bae7dc0 Shared Rabbit Connection: SimpleConnection@4f2b503c [delegate=amqp://test@ip-address:5672/]
2016-10-04 17:58:58,979 [main] TRACE CachingConnectionFactory:906 - Returning cached Channel: PublisherCallbackChannelImpl: AMQChannel(amqp://test@ip-address:5672/,1)
2016-10-04 17:58:58,979 [main] DEBUG RabbitAdmin:460 - Declarations finished
2016-10-04 17:58:58,980 [main] TRACE CachingConnectionFactory:402 - Cached Rabbit Channel: PublisherCallbackChannelImpl: AMQChannel(amqp://test@ip-address:5672/,1), conn: Proxy@bae7dc0 Shared Rabbit Connection: SimpleConnection@4f2b503c [delegate=amqp://test@ip-address:5672/] retrieved from cache
2016-10-04 17:58:58,981 [main] TRACE CachingConnectionFactory:439 - Found cached Rabbit Channel: Cached Rabbit Channel: PublisherCallbackChannelImpl: AMQChannel(amqp://test@ip-address:5672/,1), conn: Proxy@bae7dc0 Shared Rabbit Connection: SimpleConnection@4f2b503c [delegate=amqp://test@ip-address:5672/]
2016-10-04 17:58:58,981 [main] DEBUG PublisherCallbackChannelImpl:694 - Added listener org.springframework.amqp.rabbit.core.RabbitTemplate@82de64a
2016-10-04 17:58:58,982 [main] DEBUG RabbitTemplate:1451 - Added pending confirms for Cached Rabbit Channel: PublisherCallbackChannelImpl: AMQChannel(amqp://test@ip-address:5672/,1), conn: Proxy@bae7dc0 Shared Rabbit Connection: SimpleConnection@4f2b503c [delegate=amqp://test@ip-address:5672/] to map, size now 1
2016-10-04 17:58:58,982 [main] DEBUG RabbitTemplate:1296 - Executing callback on RabbitMQ Channel: Cached Rabbit Channel: PublisherCallbackChannelImpl: AMQChannel(amqp://test@ip-address:5672/,1), conn: Proxy@bae7dc0 Shared Rabbit Connection: SimpleConnection@4f2b503c [delegate=amqp://test@ip-address:5672/]
2016-10-04 17:58:58,982 [main] DEBUG RabbitAdmin:487 - declaring Exchange 'myexchange'
2016-10-04 17:58:59,006 [main] TRACE CachingConnectionFactory:906 - Returning cached Channel: PublisherCallbackChannelImpl: AMQChannel(amqp://test@ip-address:5672/,1)
2016-10-04 17:58:59,007 [main] INFO  TestProducer:59 - Declared/Declare-confirmed for direct exchange: myexchange
2016-10-04 17:58:59,009 [main] DEBUG TestProducer:74 - Sending to [exchange:myexchange, routing-key:mykey]:[testpayload]
2016-10-04 17:58:59,090 [main] TRACE CachingConnectionFactory:402 - Cached Rabbit Channel: PublisherCallbackChannelImpl: AMQChannel(amqp://test@ip-address:5672/,1), conn: Proxy@bae7dc0 Shared Rabbit Connection: SimpleConnection@4f2b503c [delegate=amqp://test@ip-address:5672/] retrieved from cache
2016-10-04 17:58:59,091 [main] TRACE CachingConnectionFactory:439 - Found cached Rabbit Channel: Cached Rabbit Channel: PublisherCallbackChannelImpl: AMQChannel(amqp://test@ip-address:5672/,1), conn: Proxy@bae7dc0 Shared Rabbit Connection: SimpleConnection@4f2b503c [delegate=amqp://test@ip-address:5672/]
2016-10-04 17:58:59,091 [main] DEBUG PublisherCallbackChannelImpl:694 - Added listener org.springframework.amqp.rabbit.core.RabbitTemplate@793f29ff
2016-10-04 17:58:59,091 [main] DEBUG RabbitTemplate:1451 - Added pending confirms for Cached Rabbit Channel: PublisherCallbackChannelImpl: AMQChannel(amqp://test@ip-address:5672/,1), conn: Proxy@bae7dc0 Shared Rabbit Connection: SimpleConnection@4f2b503c [delegate=amqp://test@ip-address:5672/] to map, size now 1
2016-10-04 17:58:59,091 [main] DEBUG RabbitTemplate:1296 - Executing callback on RabbitMQ Channel: Cached Rabbit Channel: PublisherCallbackChannelImpl: AMQChannel(amqp://test@ip-address:5672/,1), conn: Proxy@bae7dc0 Shared Rabbit Connection: SimpleConnection@4f2b503c [delegate=amqp://test@ip-address:5672/]
2016-10-04 17:58:59,098 [main] DEBUG RabbitTemplate:1325 - Publishing message on exchange [myexchange], routingKey = [mykey]
2016-10-04 17:58:59,104 [main] TRACE CachingConnectionFactory:906 - Returning cached Channel: PublisherCallbackChannelImpl: AMQChannel(amqp://test@ip-address:5672/,1)
2016-10-04 17:58:59,134 [AMQP Connection ip-address:5672] DEBUG TestProducer:103 - returnedMessage, replyCode: 312, replyText: NO_ROUTE
2016-10-04 17:58:59,135 [AMQP Connection ip-address:5672] TRACE CachingConnectionFactory:402 - Cached Rabbit Channel: PublisherCallbackChannelImpl: AMQChannel(amqp://test@ip-address:5672/,1), conn: Proxy@bae7dc0 Shared Rabbit Connection: SimpleConnection@4f2b503c [delegate=amqp://test@ip-address:5672/] retrieved from cache
2016-10-04 17:58:59,136 [AMQP Connection ip-address:5672] TRACE CachingConnectionFactory:439 - Found cached Rabbit Channel: Cached Rabbit Channel: PublisherCallbackChannelImpl: AMQChannel(amqp://test@ip-address:5672/,1), conn: Proxy@bae7dc0 Shared Rabbit Connection: SimpleConnection@4f2b503c [delegate=amqp://test@ip-address:5672/]
2016-10-04 17:58:59,163 [AMQP Connection ip-address:5672] DEBUG PublisherCallbackChannelImpl:694 - Added listener org.springframework.amqp.rabbit.core.RabbitTemplate@7ec6c641
2016-10-04 17:58:59,163 [AMQP Connection ip-address:5672] DEBUG RabbitTemplate:1451 - Added pending confirms for Cached Rabbit Channel: PublisherCallbackChannelImpl: AMQChannel(amqp://test@ip-address:5672/,1), conn: Proxy@bae7dc0 Shared Rabbit Connection: SimpleConnection@4f2b503c [delegate=amqp://test@ip-address:5672/] to map, size now 1
2016-10-04 17:58:59,163 [AMQP Connection ip-address:5672] DEBUG RabbitTemplate:1296 - Executing callback on RabbitMQ Channel: Cached Rabbit Channel: PublisherCallbackChannelImpl: AMQChannel(amqp://test@ip-address:5672/,1), conn: Proxy@bae7dc0 Shared Rabbit Connection: SimpleConnection@4f2b503c [delegate=amqp://test@ip-address:5672/]
2016-10-04 17:58:59,164 [AMQP Connection ip-address:5672] DEBUG RabbitAdmin:515 - declaring Queue 'myqueue'
2016-10-04 17:59:29,104 [main] DEBUG TestProducer:74 - Sending to [exchange:myexchange, routing-key:mykey]:[testpayload]

Nothing happens after this.
</code></pre>