A MessageListener is used to receive message asynchronously. Unlike the MessageConsumer.receive() call, message listener does not block the calling thread, instead it delivers messages to an application specified callback method, typically the onMessage method.
Message delivery will start once the Connection.Start() method is called. Message delivery can be stopped and resumed anytime using Connection.Stop() and Connection.Start() methods respectively.
Once the Connection.Start() method is called after setting a message listener to at least one consumer in a Session, that session becomes an Asynchronous Session. Once a Session becomes asynchronous, it is illegal to call any XMS .NET synchronous methods, for example MessageProducer.Send(). Doing so will result in an exception with WebSphere MQ reason code MQRC_HCONN_ASYNC_ACTIVE (2500).
However Session.Close is the only synchronous call that is allowed in an asynchronous session. Application can also make synchronous calls (except Session.Close) on the message listener callback method i.e. in the onMessage method.
Other than these two, connection must be stopped using Connection.Stop() method for an application to make any synchronous call. After the calls are made, connection must be resumed again using the Connection.Start() method. This will start message delivery again.
The next question that arises is: How many asynchronous message consumers can I have in a session?
The answer is: A session can have multiple asynchronous message consumers. But at any time message will be delivered only one consumer. What this practically means is, when a second message arrives while XMS .NET has called the onMessage() method of a consumer to deliver the first message, the second message will not be delivered to a consumer in the session till the onMessage() method returns. The second message will be delivered to a consumer in the session only after the onMessage() method returns. This is because a session manages message delivery to consumers using only one thread. Hence it can deliver only one message at a time and consumer could be anyone.
If an application requires concurrent message delivery i.e. all the consumers must receive messages at the same time, then application must create multiple sessions and each must have one asynchronous message consumer.
Let’s look at an example to understand further. First let’s look at the scenario where there multiple asynchronous message consumers in a session.
A session S has 3 asynchronous message consumers: AMC1, AMC2 and AMC3 which receive messages from 3 different destinations Q1, Q2 and Q3. In this as there is only one session S, there will be only message delivery thread to deliver messages to consumers AMC1, AMC2 and AMC3. Hence when session is delivering message to AMC1, the other two consumers AMC2 and AMC3 will wait even if there are messages in Q2 and Q3 ready for delivery.
Let’s look at the second case where there are multiple sessions S1, S2 and S3 each having one asynchronous message consumer AMC1, AMC2 and AMC3 respectively. In this case as there is only one consumer per session, messages will be delivered to consumers concurrently.
So if concurrent message delivery is required then go for multiple sessions.