这是由于ActiveMQ默认的连接超时时间为1分钟导致的,可以通过设置连接的Heartbeat属性来增加超时时间。以下是示例代码:
Connection connection = null;
try {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
connectionFactory.setConnectionTimeout(10000);//设置连接超时时间为10秒
connectionFactory.setTransportHeartbeat(30000);//设置心跳检测时间为30秒
connection = connectionFactory.createConnection();
connection.start();
} catch (JMSException e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}