如何暂时禁用消息侦听器 [英] How to temporarily disable a message listener

查看:156
本文介绍了如何暂时禁用消息侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

暂时禁用消息监听器的好方法是什么?我想解决的问题是:

What would be a nice and good way to temporarily disable a message listener? The problem I want to solve is:


  • 消息监听器收到JMS消息

  • 我在尝试处理邮件时遇到错误。

  • 我等待我的系统再次准备就绪,以便能够处理邮件。

  • 在我的系统准备好之前,我不再需要任何消息了,所以......

  • ...我想禁用消息监听器。

  • 我的系统已准备好再次处理。

  • 处理失败的消息,并确认JMS消息。

  • 再次启用消息监听器。

  • A JMS message is received by a message listener
  • I get an error when trying to process the message.
  • I wait for my system to get ready again to be able to process the message.
  • Until my system is ready, I don't want any more messages, so...
  • ...I want to disable the message listener.
  • My system is ready for processing again.
  • The failed message gets processed, and the JMS message gets acknowledged.
  • Enable the message listener again.

现在,我正在使用Sun App Server。我通过在MessageConsumer中将其设置为null来禁用消息侦听器,并使用setMessageListener(myOldMessageListener)再次启用它,但在此之后我不再收到任何消息。

Right now, I'm using Sun App Server. I disabled the message listener by setting it to null in the MessageConsumer, and enabled it again using setMessageListener(myOldMessageListener), but after this I don't get any more messages.

推荐答案

如果你没有从onMessage()监听器方法返回,直到你的系统准备好再次处理消息,那该怎么办?这将阻止JMS在该消费者身上传递另一条消息。

How about if you don't return from the onMessage() listener method until your system is ready to process messages again? That'll prevent JMS from delivering another message on that consumer.

这是同步情况下不调用receive()的异步等效。

That's the async equivalent of not calling receive() in a synchronous case.

对于给定的JMS会话没有多线程,因此消息管道会一直保持到onMessage()方法返回。

There's no multi-threading for a given JMS session, so the pipeline of messages is held up until the onMessage() method returns.

我不熟悉动态调用setMessageListener()的含义。 javadoc说那里有未定义的行为如果在现有侦听器或同步使用者正在使用消息时调用。如果你是从onMessage()中调用的话,那听起来就像是你遇到了那个未定义的情况。

I'm not familiar with the implications of dynamically calling setMessageListener(). The javadoc says there's undefined behavior if called "when messages are being consumed by an existing listener or sync consumer". If you're calling from within onMessage(), it sounds like you're hitting that undefined case.

有一个开始 / 停止连接级别的方法,如果这对你来说不是太粗糙。

There are start/stop methods at the Connection level, if that's not too coarse-grained for you.

这篇关于如何暂时禁用消息侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆