Spring JMS 错误处理的最佳实践

发布于 2024-11-28 18:23:10 字数 178 浏览 3 评论 0原文

我正在开发一种基于消息的服务,该服务将对所有传入请求进行排队并稍后处理它们。处理错误的最佳实践是什么。例如,将信息发送到下一个系统时出现格式错误的消息或通信错误。

通过使用事务,可以处理后者,但是当消息格式错误时,重试或保留它是没有用的。是否有针对不同场景实现不同错误处理的想法,如果是,应该如何完成?

谢谢!

I'm working on a message based service that queues all incoming requests and handles them later. What is the best practice for handling errors. For example malformed messages or communication errors when sending the information to the next system.

By using transactions it is possible to cope with the latter, however when a message is malformed there is no use to retry it nor keep it. Is there any idea to implement different error handling for different scenarios and if it is, how should it be done?

Thanks!

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

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

发布评论

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

评论(1

寒江雪… 2024-12-05 18:23:10

我认为你走在正确的道路上。这里有三种一般模式:

  • 消息有效并且可以处理

应用正常处理。

  • 该消息有效,但现在无法处理

可能处理该消息所需的某些资源不可用。在这种情况下,将事务设置为rollbackOnly,消息将被重新传递。希望您的 JMS 实现支持延迟重新传递的概念,这样您就不会在 MIA 资源再次可用之前重新处理同一消息数千次。如果没有(是的,我正在看你,WebSphere MQ),我通常做的就是将消息推送到另一个为暂时无法处理的消息保留的 JMS 队列中并提交。当 MIA 资源重新上线时,我按程序读取该队列中的所有消息,并将它们写回主[原始]队列,并在其中处理完成。

  • 该消息无效

抑制异常并提交事务。您将永远不会再看到该消息。要保留无效消息的审核跟踪:

  • 将无效消息写入队列,以便稍后进行检查。
  • 注销消息内容
  • 保留无效消息的 JMX 计数器(按类型、源队列、解析错误等划分)。

不过,要点是确保在您知道的情况下提交事务 em> 您将永远无法处理该消息。

I think you're on the right track. There's three general patterns here:

  • The message is valid and can be processed

Normal processing applies.

  • The message is valid, but cannot be processed right now

Perhaps some resource you need to process the message is unavailable. In this case, set the transaction to rollbackOnly and the message will be redelivered. Hopefully your JMS implementation supports the notion of delayed redelivery so that you're not reprocessing the same message thousands of times until your MIA resource becomes available again. If not (yeah, I'm looking at you, WebSphere MQ), what I usually do is push the message into another JMS queue reserved for temporarily unprocessable messages and commit. When the MIA resource comes back on line, I procedurally read all the messages off that queue and write them back to the main [original] queue where they're processed to completion.

  • The message is invalid

Suppress the exception and commit the transaction. You'll never see that message again. To keep an audit trail of invalid messages:

  • Write the invalid message off to a bad queue where it can be examined later on.
  • Log out the contents of the message
  • Keep a JMX Counter of Invalid Messages (broken out by type, source queue, parsing error etc.)

The main point, though, is to make sure you commit the transaction if you know you will never be able to process that message.

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