using System;
using System.Threading.Tasks;
using RabbitMQ.Client;
using Tapeti.Default;
namespace Tapeti.Connection
{
///
///
/// Implements the bridge between the RabbitMQ Client consumer and a Tapeti Consumer
///
internal class TapetiBasicConsumer : DefaultBasicConsumer
{
private readonly IConsumer consumer;
private readonly Func onRespond;
///
public TapetiBasicConsumer(IConsumer consumer, Func onRespond)
{
this.consumer = consumer;
this.onRespond = onRespond;
}
///
public override void HandleBasicDeliver(string consumerTag,
ulong deliveryTag,
bool redelivered,
string exchange,
string routingKey,
IBasicProperties properties,
ReadOnlyMemory body)
{
Task.Run(async () =>
{
try
{
var response = await consumer.Consume(exchange, routingKey, new RabbitMQMessageProperties(properties), body.ToArray());
await onRespond(deliveryTag, response);
}
catch
{
await onRespond(deliveryTag, ConsumeResult.Error);
}
});
}
}
}