2012-11-28 65 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. 

} 


}