2016-11-16 22:11:05 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Tapeti.Annotations;
|
|
|
|
|
using Tapeti.Connection;
|
|
|
|
|
using Tapeti.Default;
|
|
|
|
|
using Tapeti.Registration;
|
|
|
|
|
|
|
|
|
|
namespace Tapeti
|
|
|
|
|
{
|
|
|
|
|
public class TapetiConnection : IDisposable
|
|
|
|
|
{
|
|
|
|
|
public string HostName { get; set; } = "localhost";
|
|
|
|
|
public int Port { get; set; } = 5672;
|
|
|
|
|
public string VirtualHost { get; set; } = "/";
|
|
|
|
|
public string Username { get; set; } = "guest";
|
|
|
|
|
public string Password { get; set; } = "guest";
|
2016-11-20 13:34:50 +00:00
|
|
|
|
public string PublishExchange { get; set; } = "";
|
|
|
|
|
public string SubscribeExchange { get; set; } = "";
|
2016-11-16 22:11:05 +00:00
|
|
|
|
|
|
|
|
|
|
2016-11-20 13:34:50 +00:00
|
|
|
|
public IDependencyResolver DependencyResolver
|
2016-11-16 22:11:05 +00:00
|
|
|
|
{
|
2016-11-20 13:34:50 +00:00
|
|
|
|
get { return dependencyResolver ?? (dependencyResolver = new DefaultDependencyResolver(GetPublisher)); }
|
|
|
|
|
set { dependencyResolver = value; }
|
2016-11-16 22:11:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-11-20 13:34:50 +00:00
|
|
|
|
private IDependencyResolver dependencyResolver;
|
|
|
|
|
private List<IQueueRegistration> registrations;
|
2016-11-16 22:11:05 +00:00
|
|
|
|
private TapetiWorker worker;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-11-20 13:34:50 +00:00
|
|
|
|
public TapetiConnection WithDependencyResolver(IDependencyResolver resolver)
|
2016-11-16 22:11:05 +00:00
|
|
|
|
{
|
2016-11-20 13:34:50 +00:00
|
|
|
|
dependencyResolver = resolver;
|
2016-11-16 22:11:05 +00:00
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public TapetiConnection RegisterController(Type type)
|
|
|
|
|
{
|
|
|
|
|
var queueAttribute = type.GetCustomAttribute<QueueAttribute>();
|
|
|
|
|
if (queueAttribute == null)
|
|
|
|
|
throw new ArgumentException("Queue attribute required on class", nameof(type));
|
|
|
|
|
|
|
|
|
|
if (queueAttribute.Dynamic)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(queueAttribute.Name))
|
|
|
|
|
throw new ArgumentException("Dynamic queue attributes must not have a Name");
|
|
|
|
|
|
2016-11-20 13:34:50 +00:00
|
|
|
|
GetRegistrations().Add(new ControllerDynamicQueueRegistration(
|
|
|
|
|
DependencyResolver.Resolve<IControllerFactory>,
|
|
|
|
|
DependencyResolver.Resolve<IRoutingKeyStrategy>,
|
|
|
|
|
type, SubscribeExchange));
|
2016-11-16 22:11:05 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(queueAttribute.Name))
|
|
|
|
|
throw new ArgumentException("Non-dynamic queue attribute must have a Name");
|
|
|
|
|
|
2016-11-20 13:34:50 +00:00
|
|
|
|
GetRegistrations().Add(new ControllerQueueRegistration(
|
|
|
|
|
DependencyResolver.Resolve<IControllerFactory>,
|
|
|
|
|
type, SubscribeExchange, queueAttribute.Name));
|
2016-11-16 22:11:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-20 13:34:50 +00:00
|
|
|
|
(DependencyResolver as IDependencyInjector)?.RegisterController(type);
|
2016-11-16 22:11:05 +00:00
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public TapetiConnection RegisterAllControllers(Assembly assembly)
|
|
|
|
|
{
|
|
|
|
|
foreach (var type in assembly.GetTypes().Where(t => t.IsDefined(typeof(QueueAttribute))))
|
|
|
|
|
RegisterController(type);
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public TapetiConnection RegisterAllControllers()
|
|
|
|
|
{
|
|
|
|
|
return RegisterAllControllers(Assembly.GetCallingAssembly());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-11-20 13:34:50 +00:00
|
|
|
|
public async Task<ISubscriber> Subscribe()
|
2016-11-16 22:11:05 +00:00
|
|
|
|
{
|
|
|
|
|
if (registrations == null || registrations.Count == 0)
|
|
|
|
|
throw new ArgumentException("No controllers registered");
|
|
|
|
|
|
2016-11-20 13:34:50 +00:00
|
|
|
|
var subscriber = new TapetiSubscriber(GetWorker());
|
|
|
|
|
await subscriber.BindQueues(registrations);
|
|
|
|
|
|
|
|
|
|
return subscriber;
|
2016-11-16 22:11:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public IPublisher GetPublisher()
|
|
|
|
|
{
|
|
|
|
|
return new TapetiPublisher(GetWorker());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task Close()
|
|
|
|
|
{
|
|
|
|
|
if (worker != null)
|
|
|
|
|
{
|
|
|
|
|
await worker.Close();
|
|
|
|
|
worker = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Close().Wait();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-11-20 13:34:50 +00:00
|
|
|
|
protected List<IQueueRegistration> GetRegistrations()
|
2016-11-16 22:11:05 +00:00
|
|
|
|
{
|
2016-11-20 13:34:50 +00:00
|
|
|
|
return registrations ?? (registrations = new List<IQueueRegistration>());
|
2016-11-16 22:11:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected TapetiWorker GetWorker()
|
|
|
|
|
{
|
2016-11-20 13:34:50 +00:00
|
|
|
|
return worker ?? (worker = new TapetiWorker(
|
|
|
|
|
DependencyResolver.Resolve<IMessageSerializer>(),
|
|
|
|
|
DependencyResolver.Resolve<IRoutingKeyStrategy>())
|
2016-11-16 22:11:05 +00:00
|
|
|
|
{
|
|
|
|
|
HostName = HostName,
|
|
|
|
|
Port = Port,
|
|
|
|
|
VirtualHost = VirtualHost,
|
|
|
|
|
Username = Username,
|
2016-11-20 13:34:50 +00:00
|
|
|
|
Password = Password,
|
|
|
|
|
PublishExchange = PublishExchange
|
2016-11-16 22:11:05 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|