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

发布于 2024-11-03 19:18:55 字数 1350 浏览 5 评论 0原文

我正在使用rabbitmq java客户端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.
           }

谢谢。

I'm using rabbitmq java client 2.4.1 the newest version.

After a TCP connection lost, and still call a method on a channel over
this connection, a AlreadyClosedException will be thrown.

Is it a bug? I expected an IOException, but AlreadyClosedException I
got, and AlreadyClosedException is a RuntimeException.

If not, why all other errors cause an 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.
           }

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜光 2024-11-10 19:18:55

如果您的客户端丢失与代理的 TCP 连接,则该连接被视为“关闭”。因此,客户端库抛出 AlreadyClosedException 是适当的(而不是错误)。

换句话说,无论连接如何关闭(通过正常方式或通过意外失败),连接都被视为“关闭”。

If your client loses the TCP connection to your broker, the connection is considered "closed". Therefore it is appropriate (and not a bug) for the client library to throw an AlreadyClosedException.

In other words, a connection is considered "closed" no matter how it got closed (either through a graceful manner or through an unexpected failure).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文