来自jmsComponent:在不同的bean中设置异常Listener的区别是什么?

时间:2017-07-11 08:06:39

标签: apache-camel activemq spring-jms

我用这样的方式设置了一个带有弹簧的jms组件:

<bean id="jmsConnectionFactory"
      class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" value="tcp://localhost:61616" />
</bean>
<bean id="cachedConnectionFactory"
      class="org.springframework.jms.connection.CachingConnectionFactory">
    <property name="targetConnectionFactory" ref="jmsConnectionFactory"/>
    <property name="sessionCacheSize" value="10"/>
    <property name="exceptionListener" ref="exceptionListener"/>
</bean>
<bean id="jmsConfig"
      class="org.apache.camel.component.jms.JmsConfiguration">
    <property name="connectionFactory" ref="cachedConnectionFactory"/>
    <property name="concurrentConsumers" value="10"/>

</bean>

我注意到所有这4个bean都有exceptionListener属性。所以我想知道在不同的bean中设置exceptionListener的区别是什么。

在我的测试期间,只有CachingConnectionFactory中的设置可以工作,它可以进入我的ExceptionListener,而在其他情况下,异常将记录在其他地方,但不能进入我的代码,例外情况如下

WARN CachingConnectionFactory.onException(322)    - Encountered a JMSException - resetting the underlying JMS Connection
javax.jms.JMSException: java.io.EOFException
    at org.apache.activemq.util.JMSExceptionSupport.create(JMSExceptionSupport.java:54)
    at org.apache.activemq.ActiveMQConnection.onAsyncException(ActiveMQConnection.java:1983)
    at org.apache.activemq.ActiveMQConnection.onException(ActiveMQConnection.java:2002)
    at org.apache.activemq.transport.TransportFilter.onException(TransportFilter.java:101)
    at org.apache.activemq.transport.ResponseCorrelator.onException(ResponseCorrelator.java:126)
    at org.apache.activemq.transport.TransportFilter.onException(TransportFilter.java:101)
    at org.apache.activemq.transport.TransportFilter.onException(TransportFilter.java:101)
    at org.apache.activemq.transport.WireFormatNegotiator.onException(WireFormatNegotiator.java:160)
    at org.apache.activemq.transport.AbstractInactivityMonitor.onException(AbstractInactivityMonitor.java:314)
    at org.apache.activemq.transport.TransportSupport.onException(TransportSupport.java:96)
    at org.apache.activemq.transport.tcp.TcpTransport.run(TcpTransport.java:200)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.io.EOFException
    at java.io.DataInputStream.readInt(DataInputStream.java:392)
    at org.apache.activemq.openwire.OpenWireFormat.unmarshal(OpenWireFormat.java:275)
    at org.apache.activemq.transport.tcp.TcpTransport.readCommand(TcpTransport.java:221)
    at org.apache.activemq.transport.tcp.TcpTransport.doRun(TcpTransport.java:213)
    at org.apache.activemq.transport.tcp.TcpTransport.run(TcpTransport.java:196)
    ... 1 more

我无法理解这是如何运作的,请给出一些解释或提示。

1 个答案:

答案 0 :(得分:0)

org.springframework.jms.connection.CachingConnectionFactory.exceptionListener设置org.apache.activemq.ActiveMQConnectionFactory.exceptionListener,所以这是相同的。

org.apache.camel.component.jms.JmsConfiguration.exceptionListener设置了org.springframework.jms.listener.AbstractMessageListenerContainer.exceptionListener

  

设置JMS ExceptionListener以在发生JMSException时通知   由注册的消息侦听器或调用基础结构抛出。

所以它在任何级别上都与该侦听器的使用几乎相同,但最好将其设置为org.apache.camel.component.jms.JmsConfiguration.exceptionListener级别以由spring容器管理。