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?