Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to temporarily disable a message listener

Tags:

java

jms

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

  • 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.

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.

like image 798
davidi Avatar asked Mar 09 '09 10:03

davidi


1 Answers

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.

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

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

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.

like image 151
John M Avatar answered Sep 20 '22 12:09

John M