Reputation: 5023
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?
Upvotes: 2
Views: 1450
Reputation: 2779
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)
Upvotes: 0
Reputation: 704
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.
Upvotes: 1
Reputation: 18356
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.
Upvotes: 0