[英]What happens when an exception occurs in JMS listener
I am using Default Message Listener Container. 我正在使用默认消息侦听器容器。 I have set session transacted property true in configuration.
我在配置中将会话事务处理属性设置为true。
My onMessage()
method is this: 我的
onMessage()
方法是这样的:
public void onMessage(Message message) {
try {
// Some code here
} catch (JmsException jmse) {
log.error(jmse);
} catch (Throwable t) {
log.error(t);
}
}
As you can see I am handling the exception in a catch
block. 正如您所看到的,我正在
catch
块中处理异常。
My requirement is that if it is a JMS Exception, it should be resent, ie message redelivered to the listener/consumer as it happens when there is transaction rollback. 我的要求是,如果它是一个JMS异常,它应该重新发送,即当事务回滚时,消息重新传递给监听器/消费者。 How can that happen?
怎么会发生这种情况?
Can we manually rollback the transaction here? 我们可以在这里手动回滚交易吗? I think that is a possible solution but I dont how to do that in code.
我认为这是一个可能的解决方案,但我不知道如何在代码中这样做。
Another generic question: 另一个通用问题:
Since I am handling all the possible exceptions through a catch
block, I guess there will not be a scenario of message redelivery ie transaction rollback since I am handling all the possible exceptions through the catch
block. 由于我通过
catch
块处理所有可能的异常,我想不会有消息重新传递的情况,即事务回滚,因为我通过catch
块处理所有可能的异常。 Am I right? 我对吗?
You don't need a SessionAwareMessageListener
; 您不需要
SessionAwareMessageListener
; simply throw the exception instead of catching it and the container will rollback the delivery. 只需抛出异常而不是捕获它,容器将回滚传递。
Specifically, it will rollback if the exception is a JmsException
, RuntimeException
(or subclasses) or an Error
. 具体来说,如果异常是
JmsException
, RuntimeException
(或子类)或Error
,它将回滚。
You should generally not catch Throwable
anyway; 你通常不应该抓住
Throwable
; it's not good practice. 这不是好习惯。
EDIT 编辑
public void onMessage(Message message) {
try {
// Some code here
}
catch (JmsException jmse) {
log.error(jmse);
// Do some stuff
throw new RuntimeException(jmse); // JMSException is checked so can't throw
}
catch (RuntimeException e) {
log.error(e);
throw e;
}
catch (Exception e) {
log.error(t);
throw new RuntimeException(e);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.