RabbitMQ C# API 基于事件的消息消费
while (true)
{
BasicDeliverEventArgs e = (BasicDeliverEventArgs)Consumer.Queue.Dequeue();
IBasicProperties properties = e.BasicProperties;
byte[] body = e.Body;
Console.WriteLine("Recieved Message : " + Encoding.UTF8.GetString(body));
ch.BasicAck(e.DeliveryTag, false);
}
这就是我们通过订阅检索消息时所做的事情。我们使用 While 循环,因为我们希望 Consumer 连续侦听。如果我想让它基于偶数该怎么办。那就是当时有新消息到达队列时只有消费者应该消费该消息..或在任何此类类似事件上..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
RabbitMQ.Client.Events.EventingBasicConsumer
作为事件消费者而不是阻塞消费者。use the
RabbitMQ.Client.Events.EventingBasicConsumer
for a eventing consumer instead of a blocking one.您当前正在阻塞 Consumer.Queue.Dequeue()。如果我正确理解您的问题,您希望异步使用消息。
执行此操作的标准方法是编写自己的 IBasicConsumer (可能通过子类化 DefaultBasicConsumer) 和 将其设置为通道的消费者。
这样做的问题是,您必须非常小心在 IBasicConsumer.HandleBasicDelivery 中所做的事情。如果您使用任何同步 AMQP 方法,例如 basic.publish,您将遇到死锁。如果你做任何事情需要很长时间,你就会遇到其他一些问题。
如果您确实需要同步方法或长时间运行的操作,那么您所做的就是正确的方法。看一下 订阅;它是一个 IBasicConsumer,它使用消息并将它们放入队列中。
如果您需要更多帮助,可以在rabbitmq-discuss 邮件列表中询问。
You're currently blocking on the Consumer.Queue.Dequeue(). If I understand your question correctly, you want to asynchronously consume messages.
The standard way of doing this would be to write your own IBasicConsumer (probably by subclassing DefaultBasicConsumer) and set it as the consumer for the channel.
The trouble with this is that you have to be very careful about what you do in IBasicConsumer.HandleBasicDelivery. If you use any synchronous AMQP methods, such as basic.publish, you'll get a dead-lock. If you do anything that takes a long time, you'll run into some other problems.
If you do need synchronous methods or long-running actions, what you're doing is about the right way to do it. Have a look at Subscription; it's an IBasicConsumer that consumes messages and puts them on a queue for you.
If you need any more help, a great place to ask is the rabbitmq-discuss mailing list.
我遇到了这个问题并且找不到答案,因此创建了一个演示项目,让 RabbitMQ 订阅在收到消息时引发 .Net 事件。订阅在其自己的线程上运行,让 UI(在我的例子中)可以自由地执行操作。
我有趣地将我的项目称为 RabbitEar,因为它监听来自强大 RabbitMQ 的消息
我打算与 RabbitMQ 网站分享此内容,因此如果他们认为其有价值,他们可以在示例中包含链接/代码。
请访问 http://rabbitears.codeplex.com/
谢谢
西蒙
I had this problem and could not find an answer so created a demonstration project to have the RabbitMQ subscription raise .Net events when a message is received. The subscription runs on its own thread leaving the UI (in mycase) free to do it thing.
I amusing call my project RabbitEar as it listens out for messages from the mighty RabbitMQ
I intend to share this with the RabbitMQ site so if they think its of value they can include a link / code in there examples.
Check it out at http://rabbitears.codeplex.com/
Thanks
Simon