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

旧时模样 提交于 2019-12-02 17:42:12

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.

One major difference as per my knowledge not stated in others answers is that MessageConsumer can make use of MessageSelectors and hence has capability to consume messages that it's interested in, where as MessageListener will listen to all messages.

From the J2EE tutorial doc http://docs.oracle.com/javaee/5/tutorial/doc/bnceh.html

JMS Message Selectors
If your messaging application needs to filter the messages it receives, you can use a JMS API message selector, which allows a message consumer to specify the messages it is interested in. Message selectors assign the work of filtering messages to the JMS provider rather than to the application.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!