如何防止某些消息被重新传递?

发布于 2025-02-05 08:14:34 字数 747 浏览 3 评论 0原文

我正在使用Spring的@jmslistener从队列接收我的消息:

@JmsListener(destination = "my-destination", containerFactory = "my-container-factory")
void receive(MyClass myObject) {
...
}

另外,containerFactory具有sesstiontransacted true 。

如果侦听器方法抛出异常,则交易会向后回滚,并重新发布消息,这很棒。但是,在某些情况下,我不希望将消息重新发布,尤其是当传入JSON消息有效负载无法映射到myClass时。该映射在调用上述方法之前暗中发生,因此我不能将其包装在try-catch中。有什么方法可以完成此操作(下面的第2号)?

我尝试过:

  1. errorHandler注册containerFactory,该不重新恢复。这并不能阻止交易退回。
  2. 将侦听器方法的签名更改为接受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:

  1. Registering an Errorhandler with the ContainerFactory that does not rethrow. This does not prevent the transaction from being rolled back.
  2. Changing the signature of the listener method to accept TextMessage and then doing the mapping myself, wrapping the mapping in a try-catch. This works, but I since it's not the most elegant solution I was wondering if there is any other way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↘紸啶 2025-02-12 08:14:34

由于您需要对JSON映射错误做出反应,因此我建议注册a httpmessageconverterbean 处理此情况。

Since you need to to react to a JSON mapping error, I suggest to register a HttpMessageConverterBean which handles this case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文