Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a MessageListener and a Consumer in JMS?

I am new to JMS. As far as I understood Consumers are capable of picking messages from queue/topic. So why do you need a MessageListener because Consumers will know when they have picked up messages? What is the practical use of such a MessageListener?

Edit:From the Javadoc of MessageListener:

A MessageListener object is used to receive asynchronously delivered messages.

Each session must insure that it passes messages serially to the listener. This means that a listener assigned to one or more consumers of the same session can assume that the onMessage method is not called with the next message until the session has completed the last call.

So I am confused between the usage of the terms asynchronously and serially together. How do these two terms relate in describing the feature of MessageListener?

like image 222
Geek Avatar asked Jun 27 '13 06:06

Geek


People also ask

What is MessageListener in JMS?

A message listener is an object that acts as an asynchronous event handler for messages. This object implements the MessageListener interface, which contains one method, onMessage . In the onMessage method, you define the actions to be taken when a message arrives.

What is consumer in JMS?

A message consumer is an object that is created by a session and used for receiving messages sent to a destination. It implements the MessageConsumer interface. A message consumer allows a JMS client to register interest in a destination with a JMS provider.

What is producer and consumer in JMS?

A JMSProducer is created by a JMSContext and is used to send messages to a queue or topic. The JMSProducer object causes the creation of objects that are required to send the message. JMSConsumer. A JMSConsumer is created by a JMSContext and is used to receive messages from a topic or a queue.

Can a JMS queue have multiple consumers?

Support for multiple-consumer queues is a Message Queue feature (the JMS specification defines messaging behavior in the case of only one consumer accessing a queue). When multiple consumers access a queue, the load-balancing among them takes into account each consumer's capacity and message processing rate.


2 Answers

The difference is that MessageConsumer is used to receive messages synchronously:

MessageConsumer mc = s.createConsumer(queue);
Message msg = mc.receive();

For asynchronous delivery, we can register a MessageListener object with a message consumer:

mc.setMessageListener(new MessageListener() {
    public void onMessage(Message msg) {
        ...
    }
});
like image 126
Evgeniy Dorofeev Avatar answered Sep 23 '22 07:09

Evgeniy Dorofeev


from the docs:

For synchronous receipt, a client can request the next message from a message consumer using one of its receive methods.

For asynchronous delivery, a client can register a MessageListener object with a message consumer.

like image 38
wrm Avatar answered Sep 24 '22 07:09

wrm