Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

java - How to temporarily disable a message listener

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...