JMS 确认异步消息

发布于 2024-08-09 16:03:34 字数 185 浏览 2 评论 0原文

当我使用消息监听器时如何确认消息?

当我尝试在消息侦听器中进行确认时,出现以下错误。

A synchronous method call is not permitted when a session is being used asynchronously: 'acknowledge'

How do I acknowledge a message when I am using a message listener?

I get the following error when I try to do an acknowledge in my message listener.

A synchronous method call is not permitted when a session is being used asynchronously: 'acknowledge'

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

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

发布评论

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

评论(5

黎夕旧梦 2024-08-16 16:03:34

您正在谈论 JMS 消息确认,如 Message.acknowledge()?

这个错误看起来有点奇怪。如果您不使用事务或自动确认,我认为您需要调用该方法。如果您正在进行异步监听,那么除了 onMessage() 方法之外,您还会在哪里执行此操作?

此调用是否在与 onMessage() 调用相同的线程中完成?换句话说,是在 onMessage() 中还是在 onMessage() 调用的某个方法中?如果没有,您就违反了 JMS 的线程规则。会话和生产者/消费者以及任何进一步的内容(如消息)都不是线程安全的。您需要确保没有从多个线程接触它们。如果您正在进行 onMessage() 调用,并且以某种方式安排另一个线程来执行 Message.acknowledge() 调用,那么您应该因为线程问题而失败。如果是这样,请将该回调移回到运行 onMessage() 的同一线程上。

You're talking about JMS messages acknowledgement as in Message.acknowledge()?

That error seems a little odd. If you aren't using transactions or auto-acknowledge, I'd think you need to call that method. And if you're doing async listening, where are you doing to do it aside from the onMessage() method?

Is this call being done in the same thread that got the onMessage() call? In other words, in onMessage() or in some method called from onMessage()? If not, you're breaking the thread rules of JMS. Sessions and producers/consumers and anything further down (like Messages) aren't thread safe. You need to make sure you're not touching them from multiple threads. If you're in the middle of an onMessage() call and you somehow arrange another thread to do that Message.acknowledge() call, you deserve to fail because of the thread problem. If so, move that call back on the same thread that onMessage() is running in.

噩梦成真你也成魔 2024-08-16 16:03:34

这是队列会话的示例,

session = connection.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);

仅当

if (session.getAcknowledgeMode() == Session.CLIENT_ACKNOWLEDGE) //

我们可以

message.acknowledge();

在此处检查 Message 类 (http://java.sun.com/j2ee/1.4/docs/api/javax/jms/Message.html)

This is an example for Queue Session

session = connection.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);

Only if

if (session.getAcknowledgeMode() == Session.CLIENT_ACKNOWLEDGE) //

Then can we have

message.acknowledge();

Check the Message class here (http://java.sun.com/j2ee/1.4/docs/api/javax/jms/Message.html)

凉城凉梦凉人心 2024-08-16 16:03:34

为了为后代放大第一个答案:OP 可能创建了他的会话,并将确认模式设置为 Session.AUTO_ACKNOWLEDGE,这意味着提供程序在连接上传递消息时(用于同步传递)或在 MessageListener# 之后自动确认消息# onMessage() 被调用(用于异步传递)。

他得到了异常,因为他对 Message#acknowledge() 的显式调用在此模式下无效。正如 Buhake Sindi 指出的那样,如果您希望手动确认消息,则在设置将创建 MessageConsumer 的会话时必须选择 Session.CLIENT_ACKNOWLEDGE。然后,每次调用 Message#acknowledge() 时,当前消息以及传递到此会话/使用者的任何其他已传递但未确认的消息都将被确认回代理。

To amplify the first answer a bit for posterity: The OP probably created his session with acknowledgement mode set to Session.AUTO_ACKNOWLEDGE, which means the provider automatically acknowledges the messages as they are delivered on the connection (for synchronous delivery) or after your MessageListener#onMessage() is called (for asynchronous delivery).

He got the exception because his explicit call to Message#acknowledge() is not valid in this mode. As Buhake Sindi points out, if you wish to manually acknowledge messages, you must choose Session.CLIENT_ACKNOWLEDGE when you set up the session from which the MessageConsumer will be created. Then, each time you call Message#acknowledge(), the current message, along with any other delivered but unacknowledged messages delivered to this session/consumer, will be acknowledged back to the broker.

扎心 2024-08-16 16:03:34

根据定义,异步消息预计不会在协议级别得到确认。如果您想要确认,则必须将其构建到您的应用程序中,此时问题是您为什么不使用同步方案。

An asynchronous message, by definition, is not expected to be acknowledged at the protocol level. If you want an acknowledgement you must build it into your application, at which point the questions is why aren't you using a synchronous scheme.

夏日浅笑〃 2024-08-16 16:03:34

检查会话是否需要在会话外使用 getAcknowledgeMode() 方法进行确认,如果需要,则只需对消息本身调用acknowledge() 方法

Check to see if your session requires acknowledgement by using getAcknowledgeMode() method off the session, if it does then just call the acknowledge() method on the message itself

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