I have read that there are many ways of handling JMS(Java Messaging Service) messages, MDB(Message Driven Bean) is just one of them. So what are other possibilities beside MDBs exist in Java to program message consumers?
1 Answers
As noted in chapter 8 of the JMS 2.0 specification:
- In the simplified (i.e. JMS 2.0) API a consumer is represented by a
JMSConsumer
object and is created using one of several methods onJMSContext
. - In the classic (i.e. JMS 1.1) API a consumer is represented by a
MessageConsumer
object and is created using one of several methods onSession
. - In the domain-specific (i.e. JMS 1.0) API for point-to-point messaging a consumer is represented by a
QueueReceiver
object and is created using one of several methods onQueueSession
. - In the domain-specified (i.e. JMS 1.0) API for pub-sub messaging a consumer is represented by a
TopicSubscriber
object and is created using one of several methods onTopicSession
.
To be clear, later versions of the JMS specification are backwards compatible with earlier versions so if you using a JMS 1.1 client implementation you can use 1.1 and 1.0 APIs and if you're using a JMS 2.0 client implementation you can use 2.0, 1.1, & 1.0 APIs.
Each of these kinds of message consumers can either receive messages synchronously or asynchronously.
A MessageConsumer
, JMSConsumer
, QueueReceiver
and TopicSubscriber
can use these methods to receive a message synchronously:
Message receive()
: Returns the next message produced for the consumerMessage receive(long timeout)
: Returns the next message produced for the consumer that arrives within the specified timeout periodMessage receiveNoWait()
: Returns the next message produced for the cnsumer if one is immediately available.
Each of these kinds of message consumers can register an object that implements the JMS MessageListener
interface using the consumer's setMessageListener()
method. As messages arrive for the consumer, the provider delivers them by calling the listener’s onMessage
method.

- 29,372
- 4
- 21
- 43