ReceiveNoWait 不适用于 Apache.NMS & ActiveMQ

发布于 2024-09-29 11:55:10 字数 1292 浏览 1 评论 0原文

我在使用 ReceiveNoWait 与 Apache.NMS 时遇到问题ActiveMQ,非常简单的场景:

private static void Send(string text)
{
    var factory = new ConnectionFactory("tcp://localhost:61616/");
    using (var connection = factory.CreateConnection())
    {
        connection.Start();

        var session = connection.CreateSession();
        var queue = session.GetQueue("test");
        var producer = session.CreateProducer(queue);

        producer.Send(producer.CreateTextMessage(text));
    }
}

private static string Receive()
{
    var factory = new ConnectionFactory("tcp://localhost:61616/");
    using (var connection = factory.CreateConnection())
    {
        connection.Start();

        var session = connection.CreateSession();
        var queue = session.GetQueue("test");
        var consumer = session.CreateConsumer(queue);

        var message = (ITextMessage)consumer.ReceiveNoWait();
        return message == null ? null : message.Text;
    }
}

static void Main(string[] args)
{
    for (var i = 0; i < 100; i++)
    {
        Send(i.ToString());
    }

    while (true)
    {
        Console.WriteLine(Receive() ?? "(null)");
    }
}

说明:我向队列发送了 100 条短信,我将接收并发送 100 条短信。使用 while 循环将消息一一打印出来。但上面的代码总是打印 (null) - 我可以从管理控制台找到队列中的消息。

怎么了?

I met problem when use ReceiveNoWait with Apache.NMS & ActiveMQ, really simple scenarios:

private static void Send(string text)
{
    var factory = new ConnectionFactory("tcp://localhost:61616/");
    using (var connection = factory.CreateConnection())
    {
        connection.Start();

        var session = connection.CreateSession();
        var queue = session.GetQueue("test");
        var producer = session.CreateProducer(queue);

        producer.Send(producer.CreateTextMessage(text));
    }
}

private static string Receive()
{
    var factory = new ConnectionFactory("tcp://localhost:61616/");
    using (var connection = factory.CreateConnection())
    {
        connection.Start();

        var session = connection.CreateSession();
        var queue = session.GetQueue("test");
        var consumer = session.CreateConsumer(queue);

        var message = (ITextMessage)consumer.ReceiveNoWait();
        return message == null ? null : message.Text;
    }
}

static void Main(string[] args)
{
    for (var i = 0; i < 100; i++)
    {
        Send(i.ToString());
    }

    while (true)
    {
        Console.WriteLine(Receive() ?? "(null)");
    }
}

Explanation: I sent 100 text messages to the queue and I'm going to receive & print the messages one by one with a while loop. But the code above always print (null) - I can find the messages in the queue from admin console.

What's wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝颜夕 2024-10-06 11:55:10

此方法在 Apache.NMS.ActiveMQ 版本 1.6.2 中也无法正常工作。解决方法是使用等效方法 IMessage Receive(TimeSpan timeout);

var message = (ITextMessage)consumer.Receive( TimeSpan.Zero );

最好添加一些短超时:TimeSpan.FromMilliseconds( 100 ) 应该可以工作。

This method does not work properly also in Apache.NMS.ActiveMQ version 1.6.2. The workaround is to use equivalent method IMessage Receive(TimeSpan timeout);:

var message = (ITextMessage)consumer.Receive( TimeSpan.Zero );

It's good to add some short timeout: TimeSpan.FromMilliseconds( 100 ) should work.

贪了杯 2024-10-06 11:55:10

您等待消息到达需要多长时间?您使用什么版本的 NMS 库?您是否尝试过在最后的 while 循环中添加一点延迟,以便主线程不会占用 CPU?

问候
蒂姆.

http://fusesource.com

How long do you wait for the messages to arrive? What version of the NMS libraries are you using? Have you tried adding in a small delay in the final while loop so that the main thread doesn't hog the CPU?

Regards
Tim.

http://fusesource.com

浅沫记忆 2024-10-06 11:55:10

您正在创建一个消费者,然后直接调用 receiveNoWait()。问题是,当您创建消费者时,消息会异步发送到消费者。因此,在消费者收到任何消息之前(即使它们可能存在于服务器上),您在这里调用 receiveNoWait()

解决方案是让消费者始终打开,或者等待一段时间创建消费者后的时间,或使用 receive(timeout)

You are creating a consumer and then directly calling receiveNoWait(). The problem is that when you create a consumer, the messages are sent to the consumer asynchronously. So, here you are calling receiveNoWait() before any message gets received to the consumer (even though they may exist on the server)

The solution is either to have the consumer open all the time, or wait some time after creating the consumer, or use receive(timeout)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文