当代理连接丢失时,RabbitMQ 抛出 AlreadyClosedException 而不是 IOException

Sha*_*cer 3 rabbitmq

我正在使用rabbitmq java client 2.4.1 最新版本。

TCP 连接丢失后,仍然通过此连接调用通道上的方法,将抛出 AlreadyClosedException。

这是一个错误吗?我期待 IOException,但我得到了 AlreadyClosedException,而 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.
           }
Run Code Online (Sandbox Code Playgroud)

谢谢你。

Bri*_*lly 5

如果您的客户端失去与您的代理的 TCP 连接,则该连接将被视为“关闭”。因此,客户端库抛出一个AlreadyClosedException.

换句话说,无论连接如何关闭(通过优雅的方式或意外的失败),连接都被认为是“关闭的”。