MessageMistener和JMS中的Consumer之间有什么区别? [英] What is the difference between a MessageListener and a Consumer in JMS?

查看:267
本文介绍了MessageMistener和JMS中的Consumer之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 JMS 的新手。据我所知 消费者 能够从队列/主题中选择消息。那你为什么需要 MessageListener ,因为 消费者 将会知道他们何时收到消息?这样的 MessageListener 的实际用途是什么?

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?

编辑:来自< a href =http://docs.oracle.com/javaee/1.3/api/javax/jms/MessageListener.html\"rel =noreferrer> MessageListener的Javadoc :


MessageListener对象用于接收异步传递的
消息。

A MessageListener object is used to receive asynchronously delivered messages.

每个会话必须确保它将消息串行传递给
侦听器。这意味着分配给同一会话的一个或多个使用者
的侦听器可以假定onMessage方法在下一个消息之前没有被称为
,直到会话完成最后一次调用。

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 的功能?

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?

推荐答案

不同之处在于MessageConsumer用于同步接收消息:

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

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

对于异步传递,我们可以使用消息使用者注册MessageListener对象:

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

mc.setMessageListener(new MessageListener() {
    public void onMessage(Message msg) {
        ...
    }
});

这篇关于MessageMistener和JMS中的Consumer之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆