RabbitMQ异步支持

编程入门 行业动态 更新时间:2024-10-15 00:21:24
本文介绍了RabbitMQ异步支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

RabbitMQ .NET客户端是否具有某种异步支持?我希望能够异步连接和使用消息,但是到目前为止,还没有找到一种方法.

Does the RabbitMQ .NET client have any sort of asynchronous support? I'd like to be able to connect and consume messages asynchronously, but haven't found a way to do either so far.

(对于消费消息,我可以使用EventingBasicConsumer,但这不是完整的解决方案.)

仅提供一些背景信息,这是我目前如何使用RabbitMQ的示例(代码摘自我的博客):

Just to give some context, this is an example of how I'm working with RabbitMQ at the moment (code taken from my blog):

var factory = new ConnectionFactory() { HostName = "localhost" }; using (var connection = factory.CreateConnection()) { using (var channel = connection.CreateModel()) { channel.QueueDeclare("testqueue", true, false, false, null); var consumer = new EventingBasicConsumer(channel); consumer.Received += Consumer_Received; channel.BasicConsume("testqueue", true, consumer); Console.ReadLine(); } }

推荐答案

兔子支持使用AsyncEventingBasicConsumer类向异步消息处理程序进行分派.它的作用类似于EventingBasicConsumer,但是允许您注册一个返回Task的回调.回调被调度到,并且RabbitMQ客户端正在等待返回的Task.

Rabbit supports dispatching to asynchronous message handlers using the AsyncEventingBasicConsumer class. It works similarly to the EventingBasicConsumer, but allows you to register an callback which returns a Task. The callback is dispatched to and the returned Task is awaited by the RabbitMQ client.

var factory = new ConnectionFactory { HostName = "localhost", DispatchConsumersAsync = true }; using(var connection = cf.CreateConnection()) { using(var channel = conn.CreateModel()) { channel.QueueDeclare("testqueue", true, false, false, null); var consumer = new AsyncEventingBasicConsumer(model); consumer.Received += async (o, a) => { Console.WriteLine("Message Get" + a.DeliveryTag); await Task.Yield(); }; } Console.ReadLine(); }

更多推荐

RabbitMQ异步支持

本文发布于:2023-11-24 12:32:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1625216.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:RabbitMQ

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!