如何防止某些消息被重新传递?
我正在使用Spring的@jmslistener
从队列接收我的消息:
@JmsListener(destination = "my-destination", containerFactory = "my-container-factory")
void receive(MyClass myObject) {
...
}
另外,containerFactory
具有sesstiontransacted
true 。
如果侦听器方法抛出异常,则交易会向后回滚,并重新发布消息,这很棒。但是,在某些情况下,我不希望将消息重新发布,尤其是当传入JSON
消息有效负载无法映射到myClass
时。该映射在调用上述方法之前暗中发生,因此我不能将其包装在try-catch
中。有什么方法可以完成此操作(下面的第2号)?
我尝试过:
- 将
errorHandler
注册containerFactory
,该不重新恢复。这并不能阻止交易退回。 - 将侦听器方法的签名更改为接受
textMessage
,然后自己进行映射,将映射包装在try-catch
中。这起作用了,但是由于这不是最优雅的解决方案,所以我想知道是否有其他方法。
I'm using Spring's @JmsListener
to receive my messages from a queue:
@JmsListener(destination = "my-destination", containerFactory = "my-container-factory")
void receive(MyClass myObject) {
...
}
Also, the containerFactory
has sesstionTransacted
to true
.
If the listener method throws an exception, then the transaction is rolled back and the message is redelivered, which is great. However, there are certain cases where I would not like the message to be redelivered, particularly when the incoming JSON
message payload cannot be mapped to MyClass
. This mapping happens implicitly by Spring before the above method is called, so I can't just wrap it in a try-catch
. Is there any way to accomplish this (besides number 2 below)?
I tried:
- Registering an
Errorhandler
with theContainerFactory
that does not rethrow. This does not prevent the transaction from being rolled back. - Changing the signature of the listener method to accept
TextMessage
and then doing the mapping myself, wrapping the mapping in atry-catch
. This works, but I since it's not the most elegant solution I was wondering if there is any other way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您需要对JSON映射错误做出反应,因此我建议注册a httpmessageconverterbean 处理此情况。
Since you need to to react to a JSON mapping error, I suggest to register a HttpMessageConverterBean which handles this case.