2011-04-28 45 views
2

我正在使用rabbitmq java客戶端2.4.1最新版本。當代理連接丟失時,RabbitMQ拋出AlreadyClosedException而非IOException

TCP連接丟失後,仍然通過 此連接調用通道上的方法時,將拋出AlreadyClosedException。

這是一個錯誤?我期待一個IOException,但AlreadyClosedException得到了I ,並且AlreadyClosedException是一個RuntimeException。

如果不是,爲什麼所有其他錯誤都會導致IOException。

@Test 
public void testConnectionLost() throws IOException{ 
      ConnectionFactory factory = new ConnectionFactory(); 
      factory.setRequestedHeartbeat(60); 
      factory.setHost("<your rabbitmq host>"); 


      Connection connection = factory.newConnection(); 
      Channel channel = connection.createChannel(); 
      connection.close(); 

      try { 
        channel.queueDeclare("queueName", false, false, false, null); 
        Assert.fail("Exception expected."); 
      }catch (IOException e) { 
        //it will NOT reach here. 
        //Inner exception should be AlreadyClosedException 
        System.out.println(e); 
      }catch (AlreadyClosedException e) { 
        // it will reach here. 
        System.out.println(e); 

        //this is strange! 
        //I expected IOException , but AlreadyClosedException I got. 
        //And AlreadyClosedException is a RuntimeException. 
      } 

謝謝。

回答

4

如果您的客戶端失去與代理的TCP連接,則認爲該連接「已關閉」。因此,客戶端庫拋出AlreadyClosedException是合適的(而不是錯誤)。

換句話說,無論連接如何關閉(通過優雅的方式或通過意外的失敗),連接都被視爲「關閉」。

相關問題