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
?
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.
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.
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.
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.
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) {
...
}
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With