2016-11-17 16:33:27 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
2016-11-16 22:11:05 +00:00
|
|
|
|
using RabbitMQ.Client;
|
|
|
|
|
|
|
|
|
|
namespace Tapeti.Connection
|
|
|
|
|
{
|
|
|
|
|
public class TapetiWorker
|
|
|
|
|
{
|
|
|
|
|
public string HostName { get; set; }
|
|
|
|
|
public int Port { get; set; }
|
|
|
|
|
public string VirtualHost { get; set; }
|
|
|
|
|
public string Username { get; set; }
|
|
|
|
|
public string Password { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private IConnection connection;
|
|
|
|
|
private IModel channel;
|
2016-11-17 16:33:27 +00:00
|
|
|
|
private readonly Lazy<TaskQueue> taskQueue = new Lazy<TaskQueue>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Task Publish(object message)
|
|
|
|
|
{
|
|
|
|
|
return taskQueue.Value.Add(() =>
|
|
|
|
|
{
|
|
|
|
|
//GetChannel().BasicPublish();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void ApplyTopology(IMessageHandlerRegistration registration)
|
|
|
|
|
{
|
|
|
|
|
registration.ApplyTopology(GetChannel());
|
|
|
|
|
}
|
2016-11-16 22:11:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Task Close()
|
|
|
|
|
{
|
|
|
|
|
if (channel != null)
|
|
|
|
|
{
|
|
|
|
|
channel.Dispose();
|
|
|
|
|
channel = null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-17 16:33:27 +00:00
|
|
|
|
// ReSharper disable once InvertIf
|
2016-11-16 22:11:05 +00:00
|
|
|
|
if (connection != null)
|
|
|
|
|
{
|
|
|
|
|
connection.Dispose();
|
|
|
|
|
connection = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-11-17 16:33:27 +00:00
|
|
|
|
private IModel GetChannel()
|
2016-11-16 22:11:05 +00:00
|
|
|
|
{
|
|
|
|
|
if (channel != null)
|
|
|
|
|
return channel;
|
|
|
|
|
|
|
|
|
|
var connectionFactory = new ConnectionFactory
|
|
|
|
|
{
|
|
|
|
|
HostName = HostName,
|
|
|
|
|
Port = Port,
|
|
|
|
|
VirtualHost = VirtualHost,
|
|
|
|
|
UserName = Username,
|
|
|
|
|
Password = Password,
|
|
|
|
|
AutomaticRecoveryEnabled = true
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
connection = connectionFactory.CreateConnection();
|
|
|
|
|
channel = connection.CreateModel();
|
|
|
|
|
|
|
|
|
|
return channel;
|
|
|
|
|
}
|
2016-11-17 16:33:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private class ScheduledWorkItem
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2016-11-16 22:11:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|