简体   繁体   English

当消息可用时,JMS receiveNoWait()是否保证消息传递?

[英]Does JMS receiveNoWait() guarantee message delivery when messages are available?

Hello I am writing some kind of simple testing scenario where I execute the following source code: 您好,我正在编写某种简单的测试方案,在其中执行以下源代码:

Here is my send() method: 这是我的send()方法:

public void send() throws JMSException {

    Session session = null;
    MessageProducer producer = null;

    try {
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        Destination destination = session.createQueue("TEST.FOO");

        producer = session.createProducer(destination);
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

        byte[] uselessData = new byte[1024];

        BytesMessage message = session.createBytesMessage();
        message.writeBytes(uselessData);

        producer.send(message);

    } finally {
        producer.close();
        session.close();
    }
}

Here is my receive() method: 这是我的receive()方法:

public void receive() throws JMSException {

    Session session = null;
    MessageConsumer consumer = null;

    try {
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createQueue("TEST.FOO");
        consumer = session.createConsumer(destination);

        Message hugeMessage = consumer.receiveNoWait();

        if (hugeMessage == null) {
            System.out.println("Message was not received");
            unsucsesfullCount++;
        } else {
            if (hugeMessage instanceof BytesMessage) {
                System.out.println("Message received");
            }
        }
    } finally {
        consumer.close();
        session.close();
    }

}

I execute: 我执行:

send();
receive();

The message value after receiveNoWait() is always null . receiveNoWait()之后的消息值始终为null

My question here is does the receiveNoWait() guarantee message delivery when there are messages in the broker? 我的问题是代理中有消息时, receiveNoWait()是否保证消息传递? The send() is executed successfully so there is at least one message in the destination. send()成功执行,因此目标中至少有一条消息。

I've searched in the specification but there is not really clear definition if a message, which is available at the broker side should be explicitly received by receiveNoWait() at client side. 我在规范中进行了搜索,但是并没有真正明确的定义,是否应该由客户端的receiveNoWait()显式接收在代理方可用的消息

Also I want to ask, if receiveNoWait() does not have message available, should it trigger some refresh consumer process in the broker, so the next receiveNoWait() will receive the message? 我也想问一下,如果receiveNoWait()没有可用的消息,它是否应该触发代理中的一些刷新使用者进程,所以下一个receiveNoWait()将接收消息?

The example code I provided run on ActiveMQ but my question is more conceptual than provider specific, because I have the same observation for other JMS providers. 我提供的示例代码在ActiveMQ上运行,但是我的问题比特定于提供程序的问题更具概念性,因为我对其他JMS提供程序也有相同的看法。

No, the specification does not guarantee that any call to receiveNoWait will return a message, it might and then again it might not. 不,该规范不能保证对receiveNoWait任何调用receiveNoWait将返回一条消息,它可能会,然后可能不会。 When using receiveNoWait you must always check for a null return and act accordingly. 使用receiveNoWait您必须始终检查是否为空,并采取相应措施。

In the case of ActiveMQ the client will return a message if the broker has dispatched one to it and it is immediately available in the consumer prefetch buffer otherwise it just returns null. 对于ActiveMQ,如果代理已向其分派了一条消息,则客户端将返回一条消息,并且该消息可立即在使用者预取缓冲区中使用,否则它将返回null。

Other implementations my indeed send a poll request to the broker, Qpid JMS for instance uses an AMQP link drain request to ask that the broker send it any messages that are available for dispatch and the broker will either send them or signal that the link is drained and there are no messages ready. 其他实现确实会向代理发送轮询请求,例如Qpid JMS使用AMQP链接耗尽请求来要求代理向它发送任何可用于调度的消息,并且代理会发送它们或发出信号表明链接已耗尽并且没有消息准备好。

In short it's completely up to the client and broker how they implement receiveNoWait but no matter what you always need to account for the chance that you won't get a message returned to you from that method. 简而言之,这完全取决于客户端和代理如何实现receiveNoWait但是无论您始终需要考虑什么原因,您都不会从该方法返回消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM