2013-08-29 78 views
0

我加載Spring應用程序(使用Spring 2.5,不能升級到3.0)與Spring的JMS的背景下獨立的應用程序像下面如何停止隊列連接的DefaultMessageListenerContainer重試?

AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); 
下面

是我的applicationContext.xml現在

<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> 
    <property name="connectionFactory" ref="connectionFactory"/> 
    <property name="destinationName" value="destinationName"/> 
    <property name="messageListener" ref="jmsMessageListener" /> 
    <property name="exceptionListener" ref="exceptionListener"/> 
</bean> 

<bean id="jmsMessageListener" class="Listener"/> 

<bean id="connectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory"> 
      <property name="hostName" value="${hostName}"/> 
      <property name="port" value="${port}"/> 
      <property name="queueManager" value="${queueManager}"/> 
      <property name="transportType" value="${transportType}"/> 
      <property name="channel" value="${channel}"/> 
    </bean> 
    <bean id = "exceptionListener" class="ExceptionListener"> 

    public class ExceptionListener implements ExceptionListener{ 
    @Override 
public void onException(JMSException arg0) { 
    System.out.println("Exception"); 

} 
    } 

中,問題是默認情況下DefaultMessageListenerContainer試圖恢復與隊列的連接,直到它成功,我可以看到像下面的日誌文件中的消息

INFO [jmsContainer-1] org.springframework.jms.listener.DefaultMessageListenerContainer - Could not refresh JMS Connection - retrying in 5000 ms: javax.jms.JMSException: MQJMS2005: failed to create MQQueueManager for 'hostName:queueManager' 

但我的要求是,如果它無法連接我的應用程序上下文應停止加載,我可以報告一個錯誤,它無法連接和引發JMSException。

我嘗試使用DMLC的exceptionListener,但它的onException消息不會被觸發。

我想我可以解決這個問題,如果我以某種方式重寫DMLC的默認行爲或加載彈簧應用程序上下文時捕獲該異常。

任何人都可以建議我怎麼能達到上述任何或如果有任何其他的選擇?

回答

0

將連接工廠和JMS模板放入單獨的上下文中。加載該上下文並嘗試將消息發送到(可能是虛擬)隊列。如果成功,則使用小上下文作爲父上下文(因此bean可以看到連接工廠)創建一個新的應用程序上下文,將它設置爲configLocationsrefresh()它。

+0

感謝Gary.Looks是這樣的,有點變通辦法。我挖入了spring代碼,發現refreshConnectionUntilSuccessful()方法根本不調用異常監聽器,而是繼續重試connection.Cannot明白爲什麼春天人建立它像這樣。 。:( – SCoder

+0

)異常處理程序用於處理處理消息時的異常;它與連接到代理無關,也可以使用'JmsTemplate.receive()'而不是使用偵聽器容器,如果你想要的話 –

+0

我的要求就像我必須使用listener.Its只是DMLC實現的設計,它一直試圖連接直到成功,而不是停止/失敗,如果它不能。 – SCoder