如何以编程方式重新发送 EasyNetQ 出错的消息?
How to programmatically resend messages that have faulted with EasyNetQ?
我想知道如何在不使用 HosePipe 的情况下以编程方式重新发送 EasyNetQ 出现故障的消息,即使用其原始发送交换重新发送到其原始目标队列。
可以吗,怎么办?
我想到的解决办法是:
public static class AdvancedBusExtensions
{
public static async Task ResendErrorsAsync(this IAdvancedBus source, string errorQueueName)
{
var errorQueue = await source.QueueDeclareAsync(errorQueueName);
var message = await source.GetMessageAsync(errorQueue);
while (message != null)
{
var utf8Body = Encoding.UTF8.GetString(message.Body);
var error = JsonConvert.DeserializeObject<Error>(utf8Body);
var errorBodyBytes = Encoding.UTF8.GetBytes(error.Message);
var exchange = await source.ExchangeDeclareAsync(error.Exchange, x =>
{
// This can be adjusted to fit the exchange actual configuration
x.AsDurable(true);
x.AsAutoDelete(false);
x.WithType("topic");
});
await source.PublishAsync(exchange, error.RoutingKey, true, error.BasicProperties, errorBodyBytes);
message = await source.GetMessageAsync(errorQueue);
}
}
}
我想知道如何在不使用 HosePipe 的情况下以编程方式重新发送 EasyNetQ 出现故障的消息,即使用其原始发送交换重新发送到其原始目标队列。
可以吗,怎么办?
我想到的解决办法是:
public static class AdvancedBusExtensions
{
public static async Task ResendErrorsAsync(this IAdvancedBus source, string errorQueueName)
{
var errorQueue = await source.QueueDeclareAsync(errorQueueName);
var message = await source.GetMessageAsync(errorQueue);
while (message != null)
{
var utf8Body = Encoding.UTF8.GetString(message.Body);
var error = JsonConvert.DeserializeObject<Error>(utf8Body);
var errorBodyBytes = Encoding.UTF8.GetBytes(error.Message);
var exchange = await source.ExchangeDeclareAsync(error.Exchange, x =>
{
// This can be adjusted to fit the exchange actual configuration
x.AsDurable(true);
x.AsAutoDelete(false);
x.WithType("topic");
});
await source.PublishAsync(exchange, error.RoutingKey, true, error.BasicProperties, errorBodyBytes);
message = await source.GetMessageAsync(errorQueue);
}
}
}