我是 JMS
的新手。据我了解,消费者
能够从队列/主题中挑选消息。那么为什么需要 MessageListener
因为 Consumers
会知道他们何时收到消息?这样的 MessageListener
有什么实际用途?
编辑:来自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.
所以我对术语异步 和串行 的用法感到困惑。这两个术语在描述 MessageListener
的功能时有什么关系?
最佳答案
不同的是MessageConsumer是用来同步接收消息的:
MessageConsumer mc = s.createConsumer(queue);
Message msg = mc.receive();
对于异步传递,我们可以向消息消费者注册一个 MessageListener 对象:
mc.setMessageListener(new MessageListener() {
public void onMessage(Message msg) {
...
}
});
关于java - JMS 中的 MessageListener 和 Consumer 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17336036/