I was wondering whether I could register a listener on MessageConsumer and then call the receive() method. I know it doesn't sound like it is a practical scenario but it would be interesting to know. Does the JMS specification even allow something like that?
I know that Session is a single threaded context and the dispatch of messages by its listeners is going to be sequential but as I can imagine the thread that actually handles my registered listeners is going to do that and it has to be some thread that the JMS provider creates. That means that the thread that created the message consumer is separate and can concurrently listen for messages. Is what I just said right? What happens in that scenario?
As far as I know your scenario is not described in JMS specification. So it is left to JMS implementation. For example, in case of IBM MQ, a receive call after a message listener is attached would throw an exception with MQRC_HCONN_ASYNC_ACTIVE
return code.