gpt4 book ai didi

java - JMS CachingConnectionFactory onException 方法永远不会在 JMSException 上调用

转载 作者:行者123 更新时间:2023-12-02 02:31:17 24 4
gpt4 key购买 nike

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

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

在 Camel 上下文中,我定义了带有异常监听器的 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 block 中抛出的

producerTemplate.sendBody(endPoint, bytes)

执行进入catch block 内,但即使定义了异常监听器,也永远不会调用SingleConnectionFactory的OnException()。这个想法是最终调用resetConnection()(在OnException内部)来重置JMS连接。

最佳答案

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

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

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

然后添加 JmsExceptionListener 的 bean 定义:

<bean id="jmsExceptionListener" class="JmsExceptionListener"></bean>

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

<property name="exceptionListener" ref="jmsExceptionListener"/>

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

<property name="exceptionListener" ref="testCachingConnectionFactory" />

关于java - JMS CachingConnectionFactory onException 方法永远不会在 JMSException 上调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47036873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com