我正在为一个客户做一个项目,他正在使用RabbitMQ和RPC,我不太了解RabbitMQ,我正在努力在互联网上找到一些像样的例子。我需要实现一些异步操作,我会更好地解释自己。
在目前的状态下,我有一个生产者发送一个RPC请求,并等待一个来自消费者的回答,到目前为止一切都很好,一切都工作正常。我的问题是,我不想等待一个回答,我仍然需要一个答案,但我不想等待它在我的生产者。我会在这里张贴我的生产者和消费者的代码。
制作人
using System;
using System.Collections.Concurrent;
using System.Text;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
namespace RabbitProducer
{
public class RpcClient
{
private readonly IConnection connection;
private readonly IModel channel;
private readonly string replyQueueName;
private readonly EventingBasicConsumer consumer;
private readonly BlockingCollection<string> respQueue = new BlockingCollection<string>();
private readonly IBasicProperties props;
public RpcClient()
{
ConnectionFactory factory = new ConnectionFactory() { HostName = "192.168.68.17" };
connection = factory.CreateConnection();
channel = connection.CreateModel();
channel.ConfirmSelect();
replyQueueName = channel.QueueDeclare().QueueName;
consumer = new EventingBasicConsumer(channel);
props = channel.CreateBasicProperties();
string correlationId = Guid.NewGuid().ToString();
props.CorrelationId = correlationId;
props.ReplyTo = replyQueueName;
consumer.Received += (model, ea) =>
{
var body = ea.Body.ToArray();
string response = Encoding.UTF8.GetString(body);
if (ea.BasicProperties.CorrelationId == correlationId)
{
respQueue.Add(response);
}
};
channel.BasicAcks += (sender, ea) =>
{
};
channel.BasicNacks += (sender, ea) =>
{
};
}
public string Call(string message)
{
var messageBytes = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(
exchange: "",
routingKey: "Ciccio",
basicProperties: props,
body: messageBytes);
channel.BasicConsume(
consumer: consumer,
queue: replyQueueName,
autoAck: true);
return respQueue.Take();
}
public void Close()
{
connection.Close();
}
}
class Program
{
public static void Main()
{
RpcClient rpcClient = new RpcClient();
Random random = new Random();
int a = random.Next(10, 50);
Console.WriteLine("Ciccio");
Console.WriteLine(a.ToString());
string response = rpcClient.Call(a.ToString());
Console.WriteLine(" [.] Got '{0}'", response);
rpcClient.Close();
Console.ReadLine();
}
}
}
消费者
using System;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Text;
using System.Threading;
namespace RabbitConsumer
{
class Program
{
public static void Main()
{
var factory = new ConnectionFactory() { HostName = "192.168.68.17" };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "Ciccio", durable: false,
exclusive: false, autoDelete: false, arguments: null);
channel.BasicQos(0, 1, false);
var consumer = new EventingBasicConsumer(channel);
channel.BasicConsume(queue: "Ciccio",
autoAck: false, consumer: consumer);
Console.WriteLine("Ciccio");
Console.WriteLine(" [x] Awaiting RPC requests");
consumer.Received += (model, ea) =>
{
string response = null;
System.Threading.Thread.Sleep(5000);
var body = ea.Body.ToArray();
var props = ea.BasicProperties;
var replyProps = channel.CreateBasicProperties();
replyProps.CorrelationId = props.CorrelationId;
try
{
var message = Encoding.UTF8.GetString(body);
int n = int.Parse(message);
Console.WriteLine(" [.] fib({0})", message);
response = fib(n).ToString();
}
catch (Exception e)
{
Console.WriteLine(" [.] " + e.Message);
response = "";
}
finally
{
var responseBytes = Encoding.UTF8.GetBytes(response);
channel.BasicPublish(exchange: "", routingKey: props.ReplyTo,
basicProperties: replyProps, body: responseBytes);
channel.BasicAck(deliveryTag: ea.DeliveryTag,
multiple: false);
}
};
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
private static int fib(int n)
{
if (n == 0 || n == 1)
{
return n;
}
return fib(n - 1) + fib(n - 2);
}
}
}
2条答案
按热度按时间kuhbmx9i1#
好吧,显然我最近工作太多了,我忘记了正确使用任务......这就是你如何才能达到我所寻找的。
异步生产商
6jygbczu2#
完整的
async
方法是使用TaskCompletionSource
。请参阅https://gigi.nullneuron.net/gigilabs/abstracting-rabbitmq-rpc-with-taskcompletionsource/以取得范例。通过这种方式,逻辑可以抽象到一个中心位置,并在整个应用中使用,就像使用
HttpClient.GetAsync()
一样。类似于: