如何使用Java将错误消息移至IBM MQ Dead Letter队列?

发布于 2025-01-24 05:28:53 字数 205 浏览 0 评论 0原文

当前,我的程序正在处理从队列接收到的消息,但是我们遇到了一个有错误的XML文件,发生的事情是它不断循环在相同的消息上并重试该处理。

当这样的消息再次出现时,我想将消息移至“死信”队列。

我现在所做的是,我创建了一个将“ producter.send.send(destination,msg)”的类创建为死的队列,并在try-catch上调用此功能,但看来它不起作用。

Currently, my program is processing the messages being received from a queue but we encountered a xml file that has an error and what happens is it keeps looping on the same message and retrying to process it.

I would like to move the message to dead letter queue when a message like this occurs again.

What I did right now is that I created a class that will "producer.send(destination, msg)" to the dead queue and call this function on the try-catch but it seems that it is not working.

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

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

发布评论

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

评论(1

抱猫软卧 2025-01-31 05:28:53

正如@JoshMC暗示,您应该将错误消息视为毒药消息。为此,您需要启用交易,并为错误消息调用回滚。

IE。逻辑看起来像是


// Create a connection factory
JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);

JmsConnectionFactory cf = ff.createConnectionFactory();

//Set connection properties
...

context = cf.createContext(JMSContext.SESSION_TRANSACTED);
try {
    ... 
    // Message Processing
    ...
    // All is OK
    context.commit();
} catch (Exception e) {
    // Message processing failed
    context.rollback();
}
                                                                

设置了后备队列和后备阈值,则在Bothresh尝试处理该消息后,将毒物消息放在后备队列(BOQNAME)上。

所有这些都是通过基础MQ客户端代码为您完成的。

本文中有一个解释 - https://developer.ibm.com/articles/an-indroduction-to-local-transactions-using-mq-and-jms/

它也链接到此处的示例代码 - https://github.com/ibm-messaging/ibm-messaging/mq-dev-patterns /tree/master/transactions/jms/se

As @JoshMc hinted you should be treating the error messages as poison messages. For that you will need to enable transactions, and invoke a rollback for the error message.

ie. logic that looks like


// Create a connection factory
JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);

JmsConnectionFactory cf = ff.createConnectionFactory();

//Set connection properties
...

context = cf.createContext(JMSContext.SESSION_TRANSACTED);
try {
    ... 
    // Message Processing
    ...
    // All is OK
    context.commit();
} catch (Exception e) {
    // Message processing failed
    context.rollback();
}
                                                                

If a backout queue and backout threshold is set then the poison message is put on to the backout queue (BOQNAME) after BOTHRESH attempts at handling the message.

All this is done for you, by the underlying MQ Client code.

There is an explanation in this article - https://developer.ibm.com/articles/an-introduction-to-local-transactions-using-mq-and-jms/

which also links to sample code here - https://github.com/ibm-messaging/mq-dev-patterns/tree/master/transactions/JMS/SE

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