C# Rabbitmq RPC模式服务器超时解决方案

利用2个task实现,看红字,直接看代码
public string Call(string rpc_queue, byte[] obj)
{
string rstr = “no”;
var channel = this.GetModel(RpcChannel); channel.ContinuationTimeout = TimeSpan.FromSeconds(10);
channel.BasicPublish(exchange: “”, routingKey: rpc_queue, basicProperties: props, body: obj);
channel.BasicConsume(consumer: consumer, queue: replyQueueName, autoAck: true);
Task[] tasks = new Task[2];
tasks[0] = Task.Run(async () =>
{
await Task.Delay(6000);//超时
respQueue.Add(rstr);
//respQueue.CompleteAdding();
});
tasks[1] = Task.Run(() =>
{
rstr = respQueue.Take();
});
try
{
Task.WaitAll(tasks);
}
catch (AggregateException ae)
{
foreach (var ex in ae.InnerExceptions)
Console.WriteLine(” {0}: {1}”, ex.GetType().Name, ex.Message);
}
return rstr;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注