ReceiveNoWait 不适用于 Apache.NMS & ActiveMQ
我在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此方法在
Apache.NMS.ActiveMQ
版本1.6.2
中也无法正常工作。解决方法是使用等效方法IMessage Receive(TimeSpan timeout);
:最好添加一些短超时:
TimeSpan.FromMilliseconds( 100 )
应该可以工作。This method does not work properly also in
Apache.NMS.ActiveMQ
version1.6.2
. The workaround is to use equivalent methodIMessage Receive(TimeSpan timeout);
:It's good to add some short timeout:
TimeSpan.FromMilliseconds( 100 )
should work.您等待消息到达需要多长时间?您使用什么版本的 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
您正在创建一个消费者,然后直接调用
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 callingreceiveNoWait()
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)