2012-11-28 66 views
2

我想捕获由于代理关闭而无法建立的ActiveMQ连接的异常。针对Spring bean初始化的ActiveMQ连接故障转移检测

有了下面的代码:

String url = ActiveMQConnection.DEFAULT_BROKER_URL;     
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); 
Connection connection = connectionFactory.createConnection(); 
connection.start(); 

连接到代理的尝试进入无限循环,如果代理已关闭。如果我改变的URL

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

当经纪人启动时,您自动连接。你为什么这个兄弟。 – Suranga

+0

基本上我想抓住这个失败的例外,并通知有人可以采取行动,以检查为什么经纪人倒闭? – Vishal

回答

0

您可以在监听器类中实现ExceptionListener并覆盖onException消息。在那里你可以处理你的通知逻辑。它尝试自动重新连接。

public class QueueListener implements MessageListener,ExceptionListener{ 

public void onMessage(Message message) { 

} 

public void onException(JMSException jsme) { 

// Send my notifcation here. 

} 


}