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-12-05 07:00:09 +00:00
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (dependencyResolver == null)
|
|
|
|
|
DependencyResolver = new DefaultDependencyResolver();
|
|
|
|
|
|
|
|
|
|
return dependencyResolver;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
dependencyResolver = value;
|
|
|
|
|
|
|
|
|
|
var dependencyInjector = value as IDependencyInjector;
|
|
|
|
|
dependencyInjector?.RegisterPublisher(GetPublisher());
|
|
|
|
|
}
|
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-12-05 07:00:09 +00:00
|
|
|
|
DependencyResolver = resolver;
|
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-12-05 07:00:09 +00:00
|
|
|
|
var subscriber = new TapetiSubscriber(() => worker.Value);
|
2016-11-21 19:54:29 +00:00
|
|
|
|
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-12-05 07:00:09 +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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|