【问题标题】:JMS CachingConnectionFactory onException method is never called on JMSExceptionJMS CachingConnectionFactory onException 方法永远不会在 JMSException 上调用
【发布时间】:2018-04-12 17:01:00
【问题描述】:

我正在使用带有 Spring 的 Apache Camel 从我的 Java 服务发送消息。我需要重置 JMS 连接,以防交换时发生任何错误。我正在使用下面的代码来实现我的目标。

try
{
    producerTemplate.sendBody(endPoint, bytes);
}
catch (final RuntimeCamelException exception)
{
    LOGGER.error("Exception occured in sendBody", exception.getMessage(), exception);
    handleError(); // handle error here.
}

在骆驼上下文中,我定义了带有异常侦听器的 CachingConnectionFactory 并使 reconnectOnException=true

<bean id="testConnectionFactory" class="org.apache.qpid.jms.JmsConnectionFactory">
    <property name="username" value="${user.name}" />
    <property name="password" value="${user.password}" />
    <property name="clientID" value="${host.address}" />
    <property name="remoteURI"
        value="amqp://${host.address}:${host.port}?jms.clientID=${host.address}?jms.username=${user.name}&amp;jms.password=${user.password}&amp;jms.redeliveryPolicy.maxRedeliveries=${message.retry.count}&amp;amqp.saslMechanisms=PLAIN" />
</bean>

<bean id="testCachingConnectionFactory"
    class="org.springframework.jms.connection.CachingConnectionFactory">
            <property name="exceptionListener" ref="testCachingConnectionFactory" />
            <property name="targetConnectionFactory" ref="testConnectionFactory" />
            <property name="reconnectOnException" value="true" />
</bean>

在我的例子中,JMSSecurityException 是从下一行的 try 块中抛出的

producerTemplate.sendBody(endPoint, bytes)

执行进入 catch 块,但即使定义了 exceptionListener,也永远不会调用 SingleConnectionFactory 的 OnException()。这个想法是最终调用 resetConnection()(在 OnException 中)来重置 JMS 连接。

【问题讨论】:

    标签: java apache-camel spring-jms qpid jms-topic


    【解决方案1】:

    实现ExceptionListener并将异常侦听器定义作为属性添加到您的弹簧连接工厂testCachingConnectionFactory

    例如创建一个异常监听类(组件)JmsExceptionListener

    public class JmsExceptionListener implements ExceptionListener {    
        @Override
        public void onException(JMSException exception) {
            // what ever you wanna do here!
        }
    }
    

    然后为JmsExceptionListener添加一个bean定义:

    &lt;bean id="jmsExceptionListener" class="JmsExceptionListener"&gt;&lt;/bean&gt;

    然后将定义添加为异常侦听器属性:

    &lt;property name="exceptionListener" ref="jmsExceptionListener"/&gt;

    而不是您在配置中使用的:

    &lt;property name="exceptionListener" ref="testCachingConnectionFactory" /&gt;

    【讨论】:

    • CachingConnectionFactory的父类SingleConnectionFactory本身实现了ExceptionListener...所以我们不需要创建任何自定义类。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多