2016-11-17 17:33:27 +01:00
|
|
|
|
using System;
|
2016-12-11 15:08:58 +01:00
|
|
|
|
using System.Collections.Generic;
|
2016-11-17 17:33:27 +01:00
|
|
|
|
using System.Threading.Tasks;
|
2016-11-16 23:11:05 +01:00
|
|
|
|
using RabbitMQ.Client;
|
2016-11-20 14:34:50 +01:00
|
|
|
|
using RabbitMQ.Client.Exceptions;
|
|
|
|
|
using RabbitMQ.Client.Framing;
|
2016-12-11 15:08:58 +01:00
|
|
|
|
using Tapeti.Config;
|
2016-11-20 14:34:50 +01:00
|
|
|
|
using Tapeti.Tasks;
|
2016-11-16 23:11:05 +01:00
|
|
|
|
|
|
|
|
|
namespace Tapeti.Connection
|
|
|
|
|
{
|
|
|
|
|
public class TapetiWorker
|
|
|
|
|
{
|
2016-11-21 20:54:29 +01:00
|
|
|
|
public TapetiConnectionParams ConnectionParams { get; set; }
|
2016-12-11 15:08:58 +01:00
|
|
|
|
public string Exchange { get; set; }
|
2016-11-16 23:11:05 +01:00
|
|
|
|
|
2016-12-11 15:08:58 +01:00
|
|
|
|
private readonly IDependencyResolver dependencyResolver;
|
|
|
|
|
private readonly IReadOnlyList<IMessageMiddleware> messageMiddleware;
|
2016-11-20 14:34:50 +01:00
|
|
|
|
private readonly IMessageSerializer messageSerializer;
|
|
|
|
|
private readonly IRoutingKeyStrategy routingKeyStrategy;
|
|
|
|
|
private readonly Lazy<SingleThreadTaskQueue> taskQueue = new Lazy<SingleThreadTaskQueue>();
|
2016-12-05 23:41:17 +01:00
|
|
|
|
private RabbitMQ.Client.IConnection connection;
|
2016-12-11 15:08:58 +01:00
|
|
|
|
private IModel channelInstance;
|
2016-11-20 14:34:50 +01:00
|
|
|
|
|
|
|
|
|
|
2016-12-11 15:08:58 +01:00
|
|
|
|
public TapetiWorker(IDependencyResolver dependencyResolver, IReadOnlyList<IMessageMiddleware> messageMiddleware)
|
2016-11-20 14:34:50 +01:00
|
|
|
|
{
|
2016-12-11 15:08:58 +01:00
|
|
|
|
this.dependencyResolver = dependencyResolver;
|
|
|
|
|
this.messageMiddleware = messageMiddleware;
|
|
|
|
|
messageSerializer = dependencyResolver.Resolve<IMessageSerializer>();
|
|
|
|
|
routingKeyStrategy = dependencyResolver.Resolve<IRoutingKeyStrategy>();
|
2016-11-20 14:34:50 +01:00
|
|
|
|
}
|
2016-11-17 17:33:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Task Publish(object message)
|
|
|
|
|
{
|
2016-11-20 14:34:50 +01:00
|
|
|
|
return taskQueue.Value.Add(async () =>
|
2016-11-17 17:33:27 +01:00
|
|
|
|
{
|
2016-11-20 14:34:50 +01:00
|
|
|
|
var properties = new BasicProperties();
|
|
|
|
|
var body = messageSerializer.Serialize(message, properties);
|
|
|
|
|
|
|
|
|
|
(await GetChannel())
|
2016-12-11 15:08:58 +01:00
|
|
|
|
.BasicPublish(Exchange, routingKeyStrategy.GetRoutingKey(message.GetType()), false,
|
2016-11-20 14:34:50 +01:00
|
|
|
|
properties, body);
|
|
|
|
|
}).Unwrap();
|
2016-11-17 17:33:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-12-11 15:08:58 +01:00
|
|
|
|
public Task Consume(string queueName, IEnumerable<IBinding> bindings)
|
2016-11-17 17:33:27 +01:00
|
|
|
|
{
|
2016-11-20 14:34:50 +01:00
|
|
|
|
return taskQueue.Value.Add(async () =>
|
|
|
|
|
{
|
2016-12-11 15:08:58 +01:00
|
|
|
|
(await GetChannel()).BasicConsume(queueName, false, new TapetiConsumer(this, dependencyResolver, bindings, messageMiddleware));
|
2016-11-20 14:34:50 +01:00
|
|
|
|
}).Unwrap();
|
2016-11-17 17:33:27 +01:00
|
|
|
|
}
|
2016-11-16 23:11:05 +01:00
|
|
|
|
|
|
|
|
|
|
2016-12-11 15:08:58 +01:00
|
|
|
|
public async Task Subscribe(IQueue queue)
|
2016-11-16 23:11:05 +01:00
|
|
|
|
{
|
2016-12-11 15:08:58 +01:00
|
|
|
|
var queueName = await taskQueue.Value.Add(async () =>
|
|
|
|
|
{
|
|
|
|
|
var channel = await GetChannel();
|
|
|
|
|
|
|
|
|
|
if (queue.Dynamic)
|
|
|
|
|
{
|
|
|
|
|
var dynamicQueue = channel.QueueDeclare();
|
|
|
|
|
|
|
|
|
|
foreach (var binding in queue.Bindings)
|
|
|
|
|
{
|
|
|
|
|
var routingKey = routingKeyStrategy.GetRoutingKey(binding.MessageClass);
|
|
|
|
|
channel.QueueBind(dynamicQueue.QueueName, Exchange, routingKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dynamicQueue.QueueName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
channel.QueueDeclarePassive(queue.Name);
|
|
|
|
|
return queue.Name;
|
|
|
|
|
}).Unwrap();
|
2016-11-16 23:11:05 +01:00
|
|
|
|
|
2016-12-11 15:08:58 +01:00
|
|
|
|
await Consume(queueName, queue.Bindings);
|
2016-11-20 14:34:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Task Respond(ulong deliveryTag, ConsumeResponse response)
|
|
|
|
|
{
|
|
|
|
|
return taskQueue.Value.Add(async () =>
|
2016-11-16 23:11:05 +01:00
|
|
|
|
{
|
2016-11-20 14:34:50 +01: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 23:11:05 +01:00
|
|
|
|
|
2016-11-20 14:34:50 +01:00
|
|
|
|
return taskQueue.Value.Add(() =>
|
|
|
|
|
{
|
2016-12-11 15:08:58 +01:00
|
|
|
|
if (channelInstance != null)
|
2016-11-20 14:34:50 +01:00
|
|
|
|
{
|
2016-12-11 15:08:58 +01:00
|
|
|
|
channelInstance.Dispose();
|
|
|
|
|
channelInstance = null;
|
2016-11-20 14:34:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ReSharper disable once InvertIf
|
|
|
|
|
if (connection != null)
|
|
|
|
|
{
|
|
|
|
|
connection.Dispose();
|
|
|
|
|
connection = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
taskQueue.Value.Dispose();
|
|
|
|
|
});
|
2016-11-16 23:11:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-11-20 14:34:50 +01: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>
|
|
|
|
|
private async Task<IModel> GetChannel()
|
2016-11-16 23:11:05 +01:00
|
|
|
|
{
|
2016-12-11 15:08:58 +01:00
|
|
|
|
if (channelInstance != null)
|
|
|
|
|
return channelInstance;
|
2016-11-16 23:11:05 +01:00
|
|
|
|
|
|
|
|
|
var connectionFactory = new ConnectionFactory
|
|
|
|
|
{
|
2016-11-21 20:54:29 +01:00
|
|
|
|
HostName = ConnectionParams.HostName,
|
|
|
|
|
Port = ConnectionParams.Port,
|
|
|
|
|
VirtualHost = ConnectionParams.VirtualHost,
|
|
|
|
|
UserName = ConnectionParams.Username,
|
|
|
|
|
Password = ConnectionParams.Password,
|
2016-11-20 14:34:50 +01:00
|
|
|
|
AutomaticRecoveryEnabled = true,
|
|
|
|
|
RequestedHeartbeat = 30
|
2016-11-16 23:11:05 +01:00
|
|
|
|
};
|
|
|
|
|
|
2016-11-20 14:34:50 +01:00
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
connection = connectionFactory.CreateConnection();
|
2016-12-11 15:08:58 +01:00
|
|
|
|
channelInstance = connection.CreateModel();
|
2016-11-20 14:34:50 +01:00
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
catch (BrokerUnreachableException)
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(5000);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-16 23:11:05 +01:00
|
|
|
|
|
2016-12-11 15:08:58 +01:00
|
|
|
|
return channelInstance;
|
2016-11-16 23:11:05 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|