2016-11-17 16:33:27 +00:00
|
|
|
|
using System;
|
2016-12-11 14:08:58 +00:00
|
|
|
|
using System.Collections.Generic;
|
2017-02-15 21:05:01 +00:00
|
|
|
|
using System.Linq;
|
2016-11-17 16:33:27 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2016-11-16 22:11:05 +00:00
|
|
|
|
using RabbitMQ.Client;
|
2016-11-20 13:34:50 +00:00
|
|
|
|
using RabbitMQ.Client.Exceptions;
|
|
|
|
|
using RabbitMQ.Client.Framing;
|
2016-12-11 14:08:58 +00:00
|
|
|
|
using Tapeti.Config;
|
2017-02-12 20:43:30 +00:00
|
|
|
|
using Tapeti.Helpers;
|
2016-11-20 13:34:50 +00:00
|
|
|
|
using Tapeti.Tasks;
|
2016-11-16 22:11:05 +00:00
|
|
|
|
|
|
|
|
|
namespace Tapeti.Connection
|
|
|
|
|
{
|
|
|
|
|
public class TapetiWorker
|
|
|
|
|
{
|
2017-02-21 21:08:05 +00:00
|
|
|
|
private const int ReconnectDelay = 5000;
|
|
|
|
|
private const int PublishMaxConnectAttempts = 3;
|
|
|
|
|
|
2017-02-12 20:43:30 +00:00
|
|
|
|
private readonly IConfig config;
|
2016-11-21 19:54:29 +00:00
|
|
|
|
public TapetiConnectionParams ConnectionParams { get; set; }
|
2017-07-14 10:33:09 +00:00
|
|
|
|
public IConnectionEventListener ConnectionEventListener { get; set; }
|
2016-11-16 22:11:05 +00:00
|
|
|
|
|
2016-11-20 13:34:50 +00:00
|
|
|
|
private readonly IMessageSerializer messageSerializer;
|
|
|
|
|
private readonly IRoutingKeyStrategy routingKeyStrategy;
|
2017-02-07 15:13:33 +00:00
|
|
|
|
private readonly IExchangeStrategy exchangeStrategy;
|
2016-11-20 13:34:50 +00:00
|
|
|
|
private readonly Lazy<SingleThreadTaskQueue> taskQueue = new Lazy<SingleThreadTaskQueue>();
|
2016-12-05 22:41:17 +00:00
|
|
|
|
private RabbitMQ.Client.IConnection connection;
|
2016-12-11 14:08:58 +00:00
|
|
|
|
private IModel channelInstance;
|
2016-11-20 13:34:50 +00:00
|
|
|
|
|
|
|
|
|
|
2017-02-12 20:43:30 +00:00
|
|
|
|
public TapetiWorker(IConfig config)
|
2016-11-20 13:34:50 +00:00
|
|
|
|
{
|
2017-02-12 20:43:30 +00:00
|
|
|
|
this.config = config;
|
2017-02-07 15:13:33 +00:00
|
|
|
|
|
2017-02-12 20:43:30 +00:00
|
|
|
|
messageSerializer = config.DependencyResolver.Resolve<IMessageSerializer>();
|
|
|
|
|
routingKeyStrategy = config.DependencyResolver.Resolve<IRoutingKeyStrategy>();
|
|
|
|
|
exchangeStrategy = config.DependencyResolver.Resolve<IExchangeStrategy>();
|
2016-11-20 13:34:50 +00:00
|
|
|
|
}
|
2016-11-17 16:33:27 +00:00
|
|
|
|
|
|
|
|
|
|
2017-01-31 11:01:08 +00:00
|
|
|
|
public Task Publish(object message, IBasicProperties properties)
|
2016-11-17 16:33:27 +00:00
|
|
|
|
{
|
2017-02-07 15:13:33 +00:00
|
|
|
|
return Publish(message, properties, exchangeStrategy.GetExchange(message.GetType()), routingKeyStrategy.GetRoutingKey(message.GetType()));
|
2017-01-31 11:01:08 +00:00
|
|
|
|
}
|
2016-11-20 13:34:50 +00:00
|
|
|
|
|
2017-01-31 11:01:08 +00:00
|
|
|
|
|
|
|
|
|
public Task PublishDirect(object message, string queueName, IBasicProperties properties)
|
|
|
|
|
{
|
|
|
|
|
return Publish(message, properties, "", queueName);
|
2016-11-17 16:33:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-12-11 14:08:58 +00:00
|
|
|
|
public Task Consume(string queueName, IEnumerable<IBinding> bindings)
|
2016-11-17 16:33:27 +00:00
|
|
|
|
{
|
2017-02-12 14:18:12 +00:00
|
|
|
|
if (string.IsNullOrEmpty(queueName))
|
|
|
|
|
throw new ArgumentNullException(nameof(queueName));
|
|
|
|
|
|
2016-11-20 13:34:50 +00:00
|
|
|
|
return taskQueue.Value.Add(async () =>
|
|
|
|
|
{
|
2017-10-13 11:07:41 +00:00
|
|
|
|
(await GetChannel()).BasicConsume(queueName, false, new TapetiConsumer(this, queueName, config.DependencyResolver, bindings, config.MessageMiddleware, config.CleanupMiddleware));
|
2016-11-20 13:34:50 +00:00
|
|
|
|
}).Unwrap();
|
2016-11-17 16:33:27 +00:00
|
|
|
|
}
|
2016-11-16 22:11:05 +00:00
|
|
|
|
|
|
|
|
|
|
2017-02-12 14:18:12 +00:00
|
|
|
|
public Task Subscribe(IQueue queue)
|
2016-11-16 22:11:05 +00:00
|
|
|
|
{
|
2017-02-12 14:18:12 +00:00
|
|
|
|
return taskQueue.Value.Add(async () =>
|
2016-12-11 14:08:58 +00:00
|
|
|
|
{
|
|
|
|
|
var channel = await GetChannel();
|
|
|
|
|
|
|
|
|
|
if (queue.Dynamic)
|
|
|
|
|
{
|
|
|
|
|
var dynamicQueue = channel.QueueDeclare();
|
2017-02-12 14:18:12 +00:00
|
|
|
|
(queue as IDynamicQueue)?.SetName(dynamicQueue.QueueName);
|
2016-12-11 14:08:58 +00:00
|
|
|
|
|
|
|
|
|
foreach (var binding in queue.Bindings)
|
|
|
|
|
{
|
2017-02-15 21:05:01 +00:00
|
|
|
|
if (binding.QueueBindingMode == QueueBindingMode.RoutingKey)
|
|
|
|
|
{
|
|
|
|
|
var routingKey = routingKeyStrategy.GetRoutingKey(binding.MessageClass);
|
|
|
|
|
var exchange = exchangeStrategy.GetExchange(binding.MessageClass);
|
|
|
|
|
|
|
|
|
|
channel.QueueBind(dynamicQueue.QueueName, exchange, routingKey);
|
|
|
|
|
}
|
2016-12-11 14:08:58 +00:00
|
|
|
|
|
2017-07-21 12:14:19 +00:00
|
|
|
|
(binding as IBuildBinding)?.SetQueueName(dynamicQueue.QueueName);
|
2017-01-31 11:01:08 +00:00
|
|
|
|
}
|
2016-12-11 14:08:58 +00:00
|
|
|
|
}
|
2017-02-12 14:18:12 +00:00
|
|
|
|
else
|
2017-07-21 12:14:19 +00:00
|
|
|
|
{
|
2017-02-12 14:18:12 +00:00
|
|
|
|
channel.QueueDeclarePassive(queue.Name);
|
2017-07-21 12:14:19 +00:00
|
|
|
|
foreach (var binding in queue.Bindings)
|
|
|
|
|
{
|
|
|
|
|
(binding as IBuildBinding)?.SetQueueName(queue.Name);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-11 14:08:58 +00:00
|
|
|
|
}).Unwrap();
|
2016-11-20 13:34:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Task Respond(ulong deliveryTag, ConsumeResponse response)
|
|
|
|
|
{
|
|
|
|
|
return taskQueue.Value.Add(async () =>
|
2016-11-16 22:11:05 +00:00
|
|
|
|
{
|
2016-11-20 13:34:50 +00:00
|
|
|
|
switch (response)
|
|
|
|
|
{
|
|
|
|
|
case ConsumeResponse.Ack:
|
|
|
|
|
(await GetChannel()).BasicAck(deliveryTag, false);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ConsumeResponse.Nack:
|
|
|
|
|
(await GetChannel()).BasicNack(deliveryTag, false, false);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ConsumeResponse.Requeue:
|
|
|
|
|
(await GetChannel()).BasicNack(deliveryTag, false, true);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}).Unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Task Close()
|
|
|
|
|
{
|
|
|
|
|
if (!taskQueue.IsValueCreated)
|
|
|
|
|
return Task.CompletedTask;
|
2016-11-16 22:11:05 +00:00
|
|
|
|
|
2016-11-20 13:34:50 +00:00
|
|
|
|
return taskQueue.Value.Add(() =>
|
|
|
|
|
{
|
2016-12-11 14:08:58 +00:00
|
|
|
|
if (channelInstance != null)
|
2016-11-20 13:34:50 +00:00
|
|
|
|
{
|
2016-12-11 14:08:58 +00:00
|
|
|
|
channelInstance.Dispose();
|
|
|
|
|
channelInstance = null;
|
2016-11-20 13:34:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ReSharper disable once InvertIf
|
|
|
|
|
if (connection != null)
|
|
|
|
|
{
|
|
|
|
|
connection.Dispose();
|
|
|
|
|
connection = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
taskQueue.Value.Dispose();
|
|
|
|
|
});
|
2016-11-16 22:11:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-01-31 11:01:08 +00:00
|
|
|
|
private Task Publish(object message, IBasicProperties properties, string exchange, string routingKey)
|
|
|
|
|
{
|
2017-02-12 20:43:30 +00:00
|
|
|
|
var context = new PublishContext
|
2017-01-31 11:01:08 +00:00
|
|
|
|
{
|
2017-02-12 20:43:30 +00:00
|
|
|
|
DependencyResolver = config.DependencyResolver,
|
|
|
|
|
Exchange = exchange,
|
|
|
|
|
RoutingKey = routingKey,
|
|
|
|
|
Message = message,
|
|
|
|
|
Properties = properties ?? new BasicProperties()
|
|
|
|
|
};
|
2017-01-31 11:01:08 +00:00
|
|
|
|
|
2017-02-12 20:43:30 +00:00
|
|
|
|
if (!context.Properties.IsTimestampPresent())
|
|
|
|
|
context.Properties.Timestamp = new AmqpTimestamp(new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds());
|
2017-02-12 18:15:56 +00:00
|
|
|
|
|
2017-02-12 20:43:30 +00:00
|
|
|
|
if (!context.Properties.IsDeliveryModePresent())
|
|
|
|
|
context.Properties.DeliveryMode = 2; // Persistent
|
2017-01-31 11:01:08 +00:00
|
|
|
|
|
|
|
|
|
|
2017-02-12 20:43:30 +00:00
|
|
|
|
// ReSharper disable ImplicitlyCapturedClosure - MiddlewareHelper will not keep a reference to the lambdas
|
|
|
|
|
return MiddlewareHelper.GoAsync(
|
|
|
|
|
config.PublishMiddleware,
|
|
|
|
|
async (handler, next) => await handler.Handle(context, next),
|
|
|
|
|
() => taskQueue.Value.Add(async () =>
|
|
|
|
|
{
|
|
|
|
|
var body = messageSerializer.Serialize(context.Message, context.Properties);
|
2017-02-21 21:08:05 +00:00
|
|
|
|
(await GetChannel(PublishMaxConnectAttempts)).BasicPublish(context.Exchange, context.RoutingKey, false,
|
2017-02-12 20:43:30 +00:00
|
|
|
|
context.Properties, body);
|
|
|
|
|
}).Unwrap());
|
|
|
|
|
// ReSharper restore ImplicitlyCapturedClosure
|
2017-01-31 11:01:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-20 13:34:50 +00:00
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Only call this from a task in the taskQueue to ensure IModel is only used
|
|
|
|
|
/// by a single thread, as is recommended in the RabbitMQ .NET Client documentation.
|
|
|
|
|
/// </remarks>
|
2017-02-21 21:08:05 +00:00
|
|
|
|
private async Task<IModel> GetChannel(int? maxAttempts = null)
|
2016-11-16 22:11:05 +00:00
|
|
|
|
{
|
2016-12-11 14:08:58 +00:00
|
|
|
|
if (channelInstance != null)
|
|
|
|
|
return channelInstance;
|
2016-11-16 22:11:05 +00:00
|
|
|
|
|
2017-02-21 21:08:05 +00:00
|
|
|
|
var attempts = 0;
|
2016-11-16 22:11:05 +00:00
|
|
|
|
var connectionFactory = new ConnectionFactory
|
|
|
|
|
{
|
2016-11-21 19:54:29 +00:00
|
|
|
|
HostName = ConnectionParams.HostName,
|
|
|
|
|
Port = ConnectionParams.Port,
|
|
|
|
|
VirtualHost = ConnectionParams.VirtualHost,
|
|
|
|
|
UserName = ConnectionParams.Username,
|
|
|
|
|
Password = ConnectionParams.Password,
|
2017-07-14 10:33:09 +00:00
|
|
|
|
AutomaticRecoveryEnabled = true, // The created connection is an IRecoverable
|
2016-11-20 13:34:50 +00:00
|
|
|
|
RequestedHeartbeat = 30
|
2016-11-16 22:11:05 +00:00
|
|
|
|
};
|
|
|
|
|
|
2016-11-20 13:34:50 +00:00
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
connection = connectionFactory.CreateConnection();
|
2016-12-11 14:08:58 +00:00
|
|
|
|
channelInstance = connection.CreateModel();
|
2016-11-20 13:34:50 +00:00
|
|
|
|
|
2017-02-05 22:22:34 +00:00
|
|
|
|
if (ConnectionParams.PrefetchCount > 0)
|
|
|
|
|
channelInstance.BasicQos(0, ConnectionParams.PrefetchCount, false);
|
|
|
|
|
|
2017-07-14 10:33:09 +00:00
|
|
|
|
((IRecoverable)connection).Recovery += (sender, e) => ConnectionEventListener?.Reconnected();
|
|
|
|
|
|
|
|
|
|
channelInstance.ModelShutdown += (sender, e) => ConnectionEventListener?.Disconnected();
|
|
|
|
|
|
|
|
|
|
ConnectionEventListener?.Connected();
|
2016-11-20 13:34:50 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
catch (BrokerUnreachableException)
|
|
|
|
|
{
|
2017-02-21 21:08:05 +00:00
|
|
|
|
attempts++;
|
|
|
|
|
if (maxAttempts.HasValue && attempts > maxAttempts.Value)
|
|
|
|
|
throw;
|
|
|
|
|
|
|
|
|
|
await Task.Delay(ReconnectDelay);
|
2016-11-20 13:34:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-16 22:11:05 +00:00
|
|
|
|
|
2016-12-11 14:08:58 +00:00
|
|
|
|
return channelInstance;
|
2016-11-16 22:11:05 +00:00
|
|
|
|
}
|
2017-02-12 20:43:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private class PublishContext : IPublishContext
|
|
|
|
|
{
|
|
|
|
|
public IDependencyResolver DependencyResolver { get; set; }
|
|
|
|
|
public string Exchange { get; set; }
|
|
|
|
|
public string RoutingKey { get; set; }
|
|
|
|
|
public object Message { get; set; }
|
|
|
|
|
public IBasicProperties Properties { get; set; }
|
|
|
|
|
}
|
2016-11-16 22:11:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|