2016-10-04 2260 views
0

我試圖使用發佈服務器返回回調來聲明隊列和綁定,如果沒有可用的路由,以便消息不會再次丟失。 這是因爲我的隊列是自動刪除的,並且如果我的用戶宕機,它將被刪除。rabbitmq ReturnCallback在NO_ROUTE上聲明隊列時卡住(312)

但是ReturnCallback線程卡在了admin.declareQueue(queue)的returnedMessage()中。

關於進一步調試我看到這是在停留在RabbitAdmin.declareQueue(): DeclareOk []聲明= declareQueues(信道,隊列);

雖然這個調用卡住了,我看到隊列聲明(通過控制檯檢查)。也隨後發送調用不會調用returnedMessage,因爲可能第一個returnedMessage調用還沒有返回。

我在這裏做錯了什麼?聲明返回回調的隊列/綁定是否正確?

任何幫助將非常感激。謝謝。

下面是我的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); 
       } 
      } 
     } 
    } 
} 

而且我測試的製作人是這樣的:

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

連接工廠bean是:

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

附加調試日誌以供參考:

2016-10-04 17:58:58,881 [main] INFO CachingConnectionFactory:291 - Created new connection: [email protected] [delegate=amqp://[email protected]: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://[email protected]:5672/,1) 
2016-10-04 17:58:58,975 [main] DEBUG PublisherCallbackChannelImpl:694 - Added listener [email protected] 
2016-10-04 17:58:58,976 [main] DEBUG RabbitTemplate:1451 - Added pending confirms for Cached Rabbit Channel: PublisherCallbackChannelImpl: AMQChannel(amqp://[email protected]:5672/,1), conn: [email protected] Shared Rabbit Connection: [email protected] [delegate=amqp://[email protected]: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://[email protected]:5672/,1), conn: [email protected] Shared Rabbit Connection: [email protected] [delegate=amqp://[email protected]:5672/] 
2016-10-04 17:58:58,979 [main] TRACE CachingConnectionFactory:906 - Returning cached Channel: PublisherCallbackChannelImpl: AMQChannel(amqp://[email protected]: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://[email protected]:5672/,1), conn: [email protected] Shared Rabbit Connection: [email protected] [delegate=amqp://[email protected]: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://[email protected]:5672/,1), conn: [email protected] Shared Rabbit Connection: [email protected] [delegate=amqp://[email protected]:5672/] 
2016-10-04 17:58:58,981 [main] DEBUG PublisherCallbackChannelImpl:694 - Added listener [email protected] 
2016-10-04 17:58:58,982 [main] DEBUG RabbitTemplate:1451 - Added pending confirms for Cached Rabbit Channel: PublisherCallbackChannelImpl: AMQChannel(amqp://[email protected]:5672/,1), conn: [email protected] Shared Rabbit Connection: [email protected] [delegate=amqp://[email protected]: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://[email protected]:5672/,1), conn: [email protected] Shared Rabbit Connection: [email protected] [delegate=amqp://[email protected]: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://[email protected]: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://[email protected]:5672/,1), conn: [email protected] Shared Rabbit Connection: [email protected] [delegate=amqp://[email protected]: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://[email protected]:5672/,1), conn: [email protected] Shared Rabbit Connection: [email protected] [delegate=amqp://[email protected]:5672/] 
2016-10-04 17:58:59,091 [main] DEBUG PublisherCallbackChannelImpl:694 - Added listener [email protected] 
2016-10-04 17:58:59,091 [main] DEBUG RabbitTemplate:1451 - Added pending confirms for Cached Rabbit Channel: PublisherCallbackChannelImpl: AMQChannel(amqp://[email protected]:5672/,1), conn: [email protected] Shared Rabbit Connection: [email protected] [delegate=amqp://[email protected]: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://[email protected]:5672/,1), conn: [email protected] Shared Rabbit Connection: [email protected] [delegate=amqp://[email protected]: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://[email protected]: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://[email protected]:5672/,1), conn: [email protected] Shared Rabbit Connection: [email protected] [delegate=amqp://[email protected]: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://[email protected]:5672/,1), conn: [email protected] Shared Rabbit Connection: [email protected] [delegate=amqp://[email protected]:5672/] 
2016-10-04 17:58:59,163 [AMQP Connection ip-address:5672] DEBUG PublisherCallbackChannelImpl:694 - Added listener [email protected] 
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://[email protected]:5672/,1), conn: [email protected] Shared Rabbit Connection: [email protected] [delegate=amqp://[email protected]: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://[email protected]:5672/,1), conn: [email protected] Shared Rabbit Connection: [email protected] [delegate=amqp://[email protected]: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. 
+0

有趣;我轉載了它 - 調查... –

回答

0

有趣的問題 - 由於通道在初始發送後放回緩存,當返回在該通道上傳遞時,回調內的發送獲得相同的通道,並導致通道內發生死鎖 - 執行宣佈我們仍在處理退貨。

因爲我們不知道我們是否會得到回報,所以我們無法真正推遲將頻道放回緩存中。

我會考慮是否有任何方法可以檢測到這種情況並避免它,但與此同時,最安全的做法是處理聲明並在不同的線程上重新發布。 ...

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

非常感謝@GaryRussell回覆如此迅速。我會嘗試你建議的方法。謝謝。 – Rashida

+0

嘗試了你建議的方法。解決了我的問題。謝謝! – Rashida