简体   繁体   中英

question on JMS receive()/MessageListener precedence

If I set a MessageListener object for a MessageConsumer , and then call receive() on that consumer, what will happen? Will the provider give the message to receive() , or will it continue to be blocked while the message is processed by the MessageListener object?

You will get an exception, as a Session must be in either asynchronous or synchronous modes. From 4.4.6 of the JMS specification

One consequence of the session's single-thread-of-control restriction is that a session with message listeners cannot also be used to synchronously receive messages. Either the session is dedicated to the thread of control used for delivery to message listeners or it is dedicated to a thread of control initiated by client code. It is erroneous to attempt to combine both in the same session.

I'm having a hard time imagining a situation where doing this makes sense.

Suppose that your receive() did not have precedence. Then presumably there's no effect.

Suppose that your receive() did have precedence. What would you expect to happen after your receive gets a message? Presumably until you call receive again the Message Consumer would be taking the messages? In that case your system's behaviour is effectively one huge race condition, the exact arrival times of messages causes different behaviours.

You must be able to do better than this. Have an "adaptive" MessageConsumer whose behaviour you can control perhaps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM