使用RabbitMQ;我可以实现这样的Scatter Gather类型模式:
//Publisher
List<string> responses = new List<string>();
string rpcResponseQueue = channel.QueueDeclare().QueueName;
string correlationId = Guid.NewGuid().ToString();
IBasicProperties basicProperties = channel.CreateBasicProperties();
basicProperties.ReplyTo = rpcResponseQueue;
和:
_consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message2 = Encoding.UTF8.GetString(body);
var response = JsonConvert.DeserializeObject<TResponse>(message2);
responses.Add(response);
if (responses.Count >= 2)
{
if (!callbackMapper.TryRemove(ea.BasicProperties.CorrelationId, out TaskCompletionSource<IEnumerable<TResponse>> tcs))
return;
tcs.TrySetResult(responses);
}
else
return;
};
//Consumer
channel.BasicPublish("", basicDeliveryEventArgs.BasicProperties.ReplyTo, replyBasicProperties, responseBytes);
以下是我的大众运输出版商提供的一些代码:
rabbitBusControl.Publish<IRegisterCustomer>(new
{
Address = "New Street",
Id = Guid.NewGuid(),
Preferred = true,
RegisteredUtc = DateTime.UtcNow,
Name = "Nice people LTD",
Type = 1,
DefaultDiscount = 0
});
MassTransit是否具有类似于ReplyTo的功能?我花了很多时间研究这个问题,并阅读了有关Sagas的文章,例如此处:How to implement a saga using a scatter/Gather pattern In MassTransit 3.0。但是,Sagas坚持不懈,我不打算坚持任何事情。我只希望发件人等待一些答复。