当前位置:   article > 正文

rabbitmq: [AMQP Connection 192.168.31.41:5672] ForgivingExceptionHandler - An unexpected connection

192.168.31.41

如下是获取与 RabbitMQ 连接的一段代码,示例如下:

  1. /**
  2. * 获取连接对象
  3. * @return 连接对象
  4. */
  5. public static Connection getConnection() throws IOException, TimeoutException {
  6. // 定义连接工厂
  7. ConnectionFactory factory = new ConnectionFactory();
  8. // 设置服务地址
  9. factory.setHost("192.168.31.41");
  10. // 设置端口
  11. factory.setPort(5672);
  12. // 设置账号信息,用户名,密码和虚拟主机
  13. factory.setVirtualHost("host1");
  14. factory.setUsername("shoto");
  15. factory.setPassword("abc123");
  16. // 握手超时设置需要有,因为不同机器之前的网络情况不同,因此可能导致网络超时而无法连接
  17. // factory.setHandshakeTimeout(300 * 1000);
  18. // 通过工厂获取连接
  19. Connection connection = factory.newConnection();
  20. return connection;
  21. }

在用户权限、连接信息、erlang 和 RabbitMQ 版本适配都正确的情况,运行依旧报下述错误:

  1. Exception in thread "main" java.util.concurrent.TimeoutException
  2. at com.rabbitmq.utility.BlockingCell.get(BlockingCell.java:77)
  3. at com.rabbitmq.utility.BlockingCell.uninterruptibleGet(BlockingCell.java:120)
  4. at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:36)
  5. at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:451)
  6. at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:323)
  7. at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:63)
  8. at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:177)
  9. at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1161)
  10. at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1118)
  11. at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1076)
  12. at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1236)
  13. at com.shoto.utils.ConnectionUtil.getConnection(ConnectionUtil.java:31)
  14. at com.shoto.utils.ConnectionUtil.main(ConnectionUtil.java:42)
  15. 2021-01-30 20:25:20 761 ERROR [AMQP Connection 192.168.31.41:5672] ForgivingExceptionHandler - An unexpected connection driver error occured - (124)
  16. java.net.SocketException: Socket Closed
  17. at java.net.SocketInputStream.socketRead0(Native Method)
  18. at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
  19. at java.net.SocketInputStream.read(SocketInputStream.java:171)
  20. at java.net.SocketInputStream.read(SocketInputStream.java:141)
  21. at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
  22. at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
  23. at java.io.DataInputStream.readUnsignedByte(DataInputStream.java:288)
  24. at com.rabbitmq.client.impl.Frame.readFrom(Frame.java:91)
  25. at com.rabbitmq.client.impl.SocketFrameHandler.readFrame(SocketFrameHandler.java:164)
  26. at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:604)
  27. at java.lang.Thread.run(Thread.java:748)

后来才发现是由于网络连接 RabbitMQ 超时导致的,解决办法就是在连接的时候增加如下代码,具体产生问题原因未知:

  1. // 握手超时设置需要有,因为不同机器之前的网络情况不同,因此可能导致网络超时而无法连接
  2. factory.setHandshakeTimeout(300 * 1000);

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/981586
推荐阅读
相关标签
  

闽ICP备14008679号