Spring JMS 侦听器即使出现异常也会确认 [英] Spring JMS listener acknowledging even when exception
问题描述
我正在使用 JMS 向我的 SQS 队列发送/接收消息,但是即使在使用 client_acknowledge 时出现异常,我也无法重新传递消息.如何实现这一目标?我尝试了一个简单的测试,
I am using JMS to send/receive messages to my SQS queue, however i am unable to redeliver the message when there is an exception even while using client_acknowledge. How to achieve this? I tried a simple test,
@JmsListener(destination = "test-normalqueue")
public void receiveNormalQueue(String message)
{
try {
logger.info("message received in normal queue: " + message);
throw new NullPointerException();
} catch (Exception e) {
logger.error(LoggingUtil.getStackTrace(e));;
}
}
即使在异常消息之后也不会返回队列.
Even after exception message doesnt come back to queue.
@Bean
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(getSQSConnectionFactory());
factory.setConcurrency("1-2");
factory.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
return factory;
}
推荐答案
您必须使用 DMLC 的事务.
You have to use transactions with the DMLC.
使用 Session.AUTO_ACKNOWLEDGE
和 setSessionTransacted(true)
.
如果监听器正常退出,则消息被移除.如果侦听器抛出异常,消息将回滚到队列中.
If the listener exits normally, the message is removed. If the listener throws an exception, the message will be rolled-back onto the queue.
您也可以对交易使用客户端模式,但您必须自己确认成功的消息.
You can also use client mode with transactions, but you have to acknowledge successful messages yourself.
您不必使用带有 SimpleMessageListenerContainer
的事务,但您仍然必须抛出异常才能将消息重新排队.
You don't have to use transactions with a SimpleMessageListenerContainer
but you still have to throw an exception to get the message requeued.
这篇关于Spring JMS 侦听器即使出现异常也会确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!