2016-11-16 22:11:05 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
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
|
|
|
|
|
{
|
2016-11-21 19:54:29 +00:00
|
|
|
|
public TapetiConnectionParams Params { get; set; }
|
|
|
|
|
|
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;
|
2016-11-21 19:54:29 +00:00
|
|
|
|
private readonly Lazy<List<IQueueRegistration>> registrations = new Lazy<List<IQueueRegistration>>();
|
|
|
|
|
private readonly Lazy<TapetiWorker> worker;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public TapetiConnection()
|
|
|
|
|
{
|
|
|
|
|
worker = new Lazy<TapetiWorker>(() => new TapetiWorker(
|
|
|
|
|
DependencyResolver.Resolve<IMessageSerializer>(),
|
|
|
|
|
DependencyResolver.Resolve<IRoutingKeyStrategy>())
|
|
|
|
|
{
|
|
|
|
|
ConnectionParams = Params ?? new TapetiConnectionParams(),
|
|
|
|
|
PublishExchange = PublishExchange
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-11-16 22:11:05 +00:00
|
|
|
|
|
|
|
|
|
|
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-21 19:54:29 +00:00
|
|
|
|
registrations.Value.Add(new ControllerDynamicQueueRegistration(
|
|
|
|
|
DependencyResolver.Resolve<IControllerFactory>,
|
2016-11-20 13:34:50 +00:00
|
|
|
|
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-21 19:54:29 +00:00
|
|
|
|
registrations.Value.Add(new ControllerQueueRegistration(
|
|
|
|
|
DependencyResolver.Resolve<IControllerFactory>,
|
2016-11-20 13:34:50 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-11-20 13:34:50 +00:00
|
|
|
|
public async Task<ISubscriber> Subscribe()
|
2016-11-16 22:11:05 +00:00
|
|
|
|
{
|
2016-11-21 19:54:29 +00:00
|
|
|
|
if (!registrations.IsValueCreated || registrations.Value.Count == 0)
|
2016-11-16 22:11:05 +00:00
|
|
|
|
throw new ArgumentException("No controllers registered");
|
|
|
|
|
|
2016-11-21 19:54:29 +00:00
|
|
|
|
var subscriber = new TapetiSubscriber(worker.Value);
|
|
|
|
|
await subscriber.BindQueues(registrations.Value);
|
2016-11-20 13:34:50 +00:00
|
|
|
|
|
|
|
|
|
return subscriber;
|
2016-11-16 22:11:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public IPublisher GetPublisher()
|
|
|
|
|
{
|
2016-11-21 19:54:29 +00:00
|
|
|
|
return new TapetiPublisher(worker.Value);
|
2016-11-16 22:11:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task Close()
|
|
|
|
|
{
|
2016-11-21 19:54:29 +00:00
|
|
|
|
if (worker.IsValueCreated)
|
|
|
|
|
await worker.Value.Close();
|
2016-11-16 22:11:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Close().Wait();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|