2016-11-20 13:34:50 +00:00
|
|
|
|
using System;
|
2016-12-11 14:08:58 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2016-11-20 13:34:50 +00:00
|
|
|
|
using RabbitMQ.Client;
|
2016-12-11 14:08:58 +00:00
|
|
|
|
using Tapeti.Config;
|
|
|
|
|
using Tapeti.Helpers;
|
2016-11-20 13:34:50 +00:00
|
|
|
|
|
|
|
|
|
namespace Tapeti.Connection
|
|
|
|
|
{
|
|
|
|
|
public class TapetiConsumer : DefaultBasicConsumer
|
|
|
|
|
{
|
|
|
|
|
private readonly TapetiWorker worker;
|
2016-12-11 14:08:58 +00:00
|
|
|
|
private readonly IDependencyResolver dependencyResolver;
|
|
|
|
|
private readonly IReadOnlyList<IMessageMiddleware> messageMiddleware;
|
|
|
|
|
private readonly List<IBinding> bindings;
|
2016-11-20 13:34:50 +00:00
|
|
|
|
|
|
|
|
|
|
2016-12-11 14:08:58 +00:00
|
|
|
|
public TapetiConsumer(TapetiWorker worker, IDependencyResolver dependencyResolver, IEnumerable<IBinding> bindings, IReadOnlyList<IMessageMiddleware> messageMiddleware)
|
2016-11-20 13:34:50 +00:00
|
|
|
|
{
|
|
|
|
|
this.worker = worker;
|
2016-12-11 14:08:58 +00:00
|
|
|
|
this.dependencyResolver = dependencyResolver;
|
|
|
|
|
this.messageMiddleware = messageMiddleware;
|
|
|
|
|
this.bindings = bindings.ToList();
|
2016-11-20 13:34:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override void HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey,
|
|
|
|
|
IBasicProperties properties, byte[] body)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-12-11 14:08:58 +00:00
|
|
|
|
var message = dependencyResolver.Resolve<IMessageSerializer>().Deserialize(body, properties);
|
2016-11-20 13:34:50 +00:00
|
|
|
|
if (message == null)
|
|
|
|
|
throw new ArgumentException("Empty message");
|
|
|
|
|
|
2016-12-11 14:08:58 +00:00
|
|
|
|
var handled = false;
|
|
|
|
|
foreach (var binding in bindings.Where(b => b.Accept(message)))
|
|
|
|
|
{
|
|
|
|
|
var context = new MessageContext
|
|
|
|
|
{
|
|
|
|
|
Controller = dependencyResolver.Resolve(binding.Controller),
|
|
|
|
|
Message = message
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
MiddlewareHelper.Go(messageMiddleware, (handler, next) => handler.Handle(context, next));
|
|
|
|
|
|
|
|
|
|
var result = binding.Invoke(context, message).Result;
|
|
|
|
|
if (result != null)
|
|
|
|
|
worker.Publish(result);
|
|
|
|
|
|
|
|
|
|
handled = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!handled)
|
2016-11-20 13:34:50 +00:00
|
|
|
|
throw new ArgumentException($"Unsupported message type: {message.GetType().FullName}");
|
|
|
|
|
|
|
|
|
|
worker.Respond(deliveryTag, ConsumeResponse.Ack);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
2016-12-11 14:08:58 +00:00
|
|
|
|
worker.Respond(deliveryTag, ConsumeResponse.Requeue);
|
|
|
|
|
throw;
|
2016-11-20 13:34:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-11 14:08:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected class MessageContext : IMessageContext
|
|
|
|
|
{
|
|
|
|
|
public object Controller { get; set; }
|
|
|
|
|
public object Message { get; set; }
|
|
|
|
|
public IDictionary<string, object> Items { get; } = new Dictionary<string, object>();
|
|
|
|
|
}
|
2016-11-20 13:34:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|