我的问题是关于 EventingBasicConsumer 的。我有一个功能,可以在同一连接上每秒创建一个新频道。如果达到通道限制,它会创建一个新连接并继续该过程。现在我还需要一个消费者。我的问题是,我该如何处理?我应该为每个通道使用一个消费者(如果可能的话?),还是应该为每个单独的通道创建一个 EventingBasicConsumer,就像我在下面的代码中所做的那样?
public string Caller(string methodName, params object[] args)
{
try
{
var connection = RabbitMQConnectionManager.GetConnection();
var channel = RabbitMQConnectionManager.GetChannel();
BlockingCollection<string> respQueue = new BlockingCollection<string>();
var replyQueueName = channel.QueueDeclare().QueueName;
var consumer = new EventingBasicConsumer(channel);
var props = channel.CreateBasicProperties();
var correlationId = Guid.NewGuid().ToString();
props.CorrelationId = correlationId;
props.ReplyTo = replyQueueName;
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var response = Encoding.UTF8.GetString(body.ToArray());
if (ea.BasicProperties.CorrelationId == correlationId)
{
respQueue.Add(response);
}
};
var callString = methodName;
foreach (var arg in args)
{
callString += " \"" + JsonConvert.SerializeObject(arg).Replace("\"", "'") + "\"";
}
var messageBytes = Encoding.UTF8.GetBytes(callString);
channel.BasicPublish(
exchange: Broker.Constants.QueuenameActionHandler,
routingKey: "",
basicProperties: props,
body: messageBytes);
channel.BasicConsume(
consumer: consumer,
queue: replyQueueName,
autoAck: true);
return respQueue.Take();
}
catch (Exception ex)
{
}
}
在这种情况下我怎样才能达到最佳性能?