rabbitmq ReturnCallback stuck while declaring queue on NO_ROUTE(312)

笑着哭i 提交于 2020-01-05 06:26:10

问题


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.

But the ReturnCallback thread gets stuck in returnedMessage() at admin.declareQueue(queue).

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

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

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

Any help would be really appreciated. Thanks.

Below is my ReturnCallback:

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) && 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);
                }
            }
        }
    }
}

And my test producer is something like:

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);
    }
}

ConnectionFactory bean is:

<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"/>

Attaching debug logs for reference:

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.

回答1:


Interesting problem - since the channel is put back into the cache after the initial send, when the return is delivered on that channel, the send within the callback gets the same channel and this causes a deadlock within the channel - doing the declare while we're still processing the return.

We can't really delay putting the channel back in the cache until we get the return because we don't know whether or not we will ever get a return.

I will give some thought as to whether there is any way we can detect this condition and avoid it, but in the meantime, the safest thing for you to do is process the declaration and re-publish on a different thread...

private final Executor executor = Executors.newCachedThreadPool();

@Override
public void returnedMessage(Message message, int replyCode, String replyText, String exchangeName,
        String routingKey) {
    if (replyCode == 312) {
        executor.execute(() -> {
            RabbitAdmin admin = new RabbitAdmin(connectionFactory);
            Exchange exchange = new DirectExchange(exchangeName, true, false);
            Queue queue = new Queue("foo", 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);
                System.out.println("Sending to [exchange:" + exchange.getName() + ", routing-key:" + routingKey
                        + "]:" + message.toString());
                rabbitmqTemplate.send(exchangeName, routingKey, message);
            }
        });
    }
}


来源:https://stackoverflow.com/questions/39859364/rabbitmq-returncallback-stuck-while-declaring-queue-on-no-route312

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!