我正在尝试捕获由于代理已关闭而无法建立的ActiveMQ连接的异常。
使用以下代码:
String url = ActiveMQConnection.DEFAULT_BROKER_URL;
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection();
connection.start();
如果代理已关闭,连接到代理的尝试将进入无限循环。如果我将网址更改为
String url = "failover:(tcp://127.0.0.1:61616/)?startupMaxReconnectAttempts=2";
它会进行2次尝试然后抛出异常。(这就是我想要的。)
现在,如果我使用Spring Bean使用以下内容初始化连接对象:
<bean id="jmsFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<!--<value>tcp://0.0.0.0:61616</value>-->
<value>failover:(tcp://127.0.0.1:61616/)?startupMaxReconnectAttempts=2</value>
</property>
</bean>
我在2次尝试中收到连接失败的错误消息但是,它仍然会在每5秒后再次尝试连接,再次发出相同的错误消息并继续进行无限循环。
ERROR transport.failover.FailoverTransport - Failed to connect to [tcp://127.0.0.1:61616/] after: 2 attempt(s)
WARN jms.listener.DefaultMessageListenerContainer - Could not refresh JMS Connection for destination 'destinationQueue' - retrying in 5000 ms. Cause: Connection refused
ERROR transport.failover.FailoverTransport - Failed to connect to [tcp://127.0.0.1:61616/] after: 2 attempt(s)
WARN jms.listener.DefaultMessageListenerContainer - Could not refresh JMS Connection for destination 'destinationQueue' - retrying in 5000 ms. Cause: Connection refused
ERROR transport.failover.FailoverTransport - Failed to connect to [tcp://127.0.0.1:61616/] after: 2 attempt(s)
WARN jms.listener.DefaultMessageListenerContainer - Could not refresh JMS Connection for destination 'destinationQueue' - retrying in 5000 ms. Cause: Connection refused
ERROR transport.failover.FailoverTransport - Failed to connect to [tcp://127.0.0.1:61616/] after: 2 attempt(s)
WARN jms.listener.DefaultMessageListenerContainer - Could not refresh JMS Connection for destination 'destinationQueue' - retrying in 5000 ms. Cause: Connection refused
these messages repeat!!
我想知道如何在发生故障时停止此无限轮询并捕获异常(可能正在使用PostInit)。
答案 0 :(得分:0)
您可以在侦听器类中实现ExceptionListener并覆盖onException消息。在那里你可以处理你的通知逻辑。虽然它尝试自动重新连接。
public class QueueListener implements MessageListener,ExceptionListener{
public void onMessage(Message message) {
}
public void onException(JMSException jsme) {
// Send my notifcation here.
}
}