MDB vs JMS listener

Notes around MDB ( Message Driven Bean) vs Standard JMS listener, and their advantages and limitations around concurrency, scalability etc.


JMS ListenerJMS has NO built-in mechanism to handle more than one incoming request at a time. Meaning that, if you have JMS listener which is listening a queue, where loads of messages coming in, then your JMS listener will consume one message at a time, which results in picking up messages one by one, which might cause message piling up on queue!

MDB The main advantage that MDBs have over simple JMS message consumers is that the EJB container can instantiate multiple MDB instances to handle multiple messages simultaneously.

Now, in order to handle concurrently in JMS listener you have two options:-

a) Dynamic scaling of JMS listener by spawning multiple threads for JMS listener via configuration.

This can be done by adding property concurrency="15-40" to <jms:listener-container>
This sets concurrentConsumer to 15 and maxConcurrentConsumer to 40 in the properties of DefalutMessagingListenerContainer ( DMLC ) of Spring.

Above configuration instructs DMLC to always start up a minimum of 15 consumers. When a new message has been received, if the maxConcurrentConsumers has not been reached and the value of the idleConsumerLimit property has not been reached, then a new consumer is created to process the message. 
This behavior from the DMLC continues up to the limit set by the maxConcurrentConsumers property. When no messages are being received and the consumers become idle, the number of consumers is automatically decreased

b) Deploy JMS listener on multiple JVM instances.This allows multiple consumers listening to queue.

IMP Note-> One should NOT increase number of concurrent consumers for a JMS topic. This leads to concurrent consumption of same message, which is NOT desirable.

MDB Limitation
MDB can be linked to listen from only one queue or topic whereas standard JMS listener can be linked to listen to multiple queues or topic.
In order to allow a MDB to listen to multiple queues or topic, you will have to deploy multiple MDB classes.

No comments: