赞
踩
如下是获取与 RabbitMQ 连接的一段代码,示例如下:
- /**
- * 获取连接对象
- * @return 连接对象
- */
- public static Connection getConnection() throws IOException, TimeoutException {
- // 定义连接工厂
- ConnectionFactory factory = new ConnectionFactory();
- // 设置服务地址
- factory.setHost("192.168.31.41");
- // 设置端口
- factory.setPort(5672);
- // 设置账号信息,用户名,密码和虚拟主机
- factory.setVirtualHost("host1");
- factory.setUsername("shoto");
- factory.setPassword("abc123");
- // 握手超时设置需要有,因为不同机器之前的网络情况不同,因此可能导致网络超时而无法连接
- // factory.setHandshakeTimeout(300 * 1000);
- // 通过工厂获取连接
- Connection connection = factory.newConnection();
- return connection;
- }
在用户权限、连接信息、erlang 和 RabbitMQ 版本适配都正确的情况,运行依旧报下述错误:
- Exception in thread "main" java.util.concurrent.TimeoutException
- at com.rabbitmq.utility.BlockingCell.get(BlockingCell.java:77)
- at com.rabbitmq.utility.BlockingCell.uninterruptibleGet(BlockingCell.java:120)
- at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:36)
- at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:451)
- at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:323)
- at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:63)
- at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:177)
- at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1161)
- at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1118)
- at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1076)
- at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1236)
- at com.shoto.utils.ConnectionUtil.getConnection(ConnectionUtil.java:31)
- at com.shoto.utils.ConnectionUtil.main(ConnectionUtil.java:42)
- 2021-01-30 20:25:20 761 ERROR [AMQP Connection 192.168.31.41:5672] ForgivingExceptionHandler - An unexpected connection driver error occured - (124)
- java.net.SocketException: Socket Closed
- at java.net.SocketInputStream.socketRead0(Native Method)
- at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
- at java.net.SocketInputStream.read(SocketInputStream.java:171)
- at java.net.SocketInputStream.read(SocketInputStream.java:141)
- at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
- at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
- at java.io.DataInputStream.readUnsignedByte(DataInputStream.java:288)
- at com.rabbitmq.client.impl.Frame.readFrom(Frame.java:91)
- at com.rabbitmq.client.impl.SocketFrameHandler.readFrame(SocketFrameHandler.java:164)
- at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:604)
- at java.lang.Thread.run(Thread.java:748)
后来才发现是由于网络连接 RabbitMQ 超时导致的,解决办法就是在连接的时候增加如下代码,具体产生问题原因未知:
- // 握手超时设置需要有,因为不同机器之前的网络情况不同,因此可能导致网络超时而无法连接
- factory.setHandshakeTimeout(300 * 1000);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。