tags:

views:

11

answers:

1

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?

A: 

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

Tim Bish
I've add a 0.5 second delay after each <code>Receive</code> call but still get the same result. I'm using the latest 1.4.x version of Apache.NMS and Apache.NMS.ActiveMQ.
Jeffrey Zhao
I'd recommend you create an NUnit test that reproduces the problem case and open a new issue at the ActiveMQ.NET Jira. Or at least take the question to the ActiveMQ Mailing lists.
Tim Bish