我正在收听activemq的队列。
我的配置如下:
ACTIVEMQ_BROKER_URL =failover:(tcp://LON6QAEQTAPP01:61616?wireFormat.maxInactivityDuration=0)
从控制台日志中我可以看到重新连接尝试连接到此服务器。
[2014-08-20 08:57:43,303] INFO 236 [ActiveMQ Task-1] - org.apache.activemq.transport.failover.FailoverTransport.doReconnect(FailoverTransport.java:1030) - 已成功连接到tcp:// LON6QAEQTAPP01:61616?wireFormat.maxInactivityDuration = 0 [2014-08-20 08:57:43,455] INFO 288 [ActiveMQ Task-1] - org.apache.activemq.transport.failover.FailoverTransport.doReconnect(FailoverTransport.java:1030) - 已成功连接到tcp:// LON6QAEQTAPP01:61616?wireFormat.maxInactivityDuration = 0 [2014-08-20 08:57:43,474] INFO 307 [ActiveMQ Task-1] - org.apache.activemq.transport.failover.FailoverTransport.doReconnect(FailoverTransport.java:1030) - 已成功连接到tcp:// LON6QAEQTAPP01:61616?wireFormat.maxInactivityDuration = 0
我仍然无法使用该消息。我正在使用以下代码尝试从代码端处理它,但仍然无法使连接持久。
try
{
connection.start();
while (true)
{
try
{
if (connection.isClosed() || connection.isTransportFailed() || session.isClosed() || !connection.getTransport().isConnected())
{
log.info("Connection was reset, re-connecting..");
connection.cleanup();
init();
connection.start();
}
}
catch (Throwable t)
{
init();
log.error("Connection was reset, re-connecting in exception block", t);
}
Thread.sleep(30000L);
}
private void init() throws Exception
{
init(brokerURL, queueName, userName, password, manual);
}
public void init(String brokerURL, String queueName, String userName, String password, boolean manual) throws Exception
{
this.manual = manual;
this.brokerURL = brokerURL;
this.queueName = queueName;
this.userName = userName;
this.password = password;
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(userName, password, brokerURL);
connection = (ActiveMQConnection) connectionFactory.createConnection();
connection.addTransportListener(new TransportListener()
{
@Override
public void transportResumed()
{
log.info("Transport Resumed ");
}
@Override
public void transportInterupted()
{
log.info("Transport Interupted ");
}
@Override
public void onException(IOException arg0)
{
arg0.printStackTrace();
log.error("Transport Exception: " + arg0.getMessage());
}
@Override
public void onCommand(Object arg0)
{
// TODO Auto-generated method stub
}
});
connection.setExceptionListener(this);
connection.setCloseTimeout(10);
session = (ActiveMQSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE);// CLIENT_ACKNOWLEDGE
Destination destination = session.createQueue(queueName);
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(this);
}
有人可以帮助我,我怎样才能让这个连接始终保持活跃状态。它通常在40分钟左右不活动后超时。
答案 0 :(得分:0)
试图以你的方式强制重新连接一个开放的连接绝对不会对你有好处。如果您想自己处理连接中断,则需要拆除连接并创建一个新连接。你真正应该做的是弄清楚什么是关闭你的连接,可能是关闭非活动连接或负载均衡器的防火墙。查看您的环境,看看混合中还有哪些可能会关闭您的连接。
如果配置为代理,则代理可以重新平衡群集中的客户端。您是否在群集环境中使用多个客户端等?