2016-11-16 22:11:05 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
2016-12-11 14:08:58 +00:00
|
|
|
|
using Tapeti.Config;
|
2016-11-16 22:11:05 +00:00
|
|
|
|
using Tapeti.Connection;
|
|
|
|
|
|
|
|
|
|
namespace Tapeti
|
|
|
|
|
{
|
|
|
|
|
public class TapetiConnection : IDisposable
|
|
|
|
|
{
|
2016-12-11 14:08:58 +00:00
|
|
|
|
private readonly IConfig config;
|
2016-11-21 19:54:29 +00:00
|
|
|
|
public TapetiConnectionParams Params { get; set; }
|
|
|
|
|
|
|
|
|
|
private readonly Lazy<TapetiWorker> worker;
|
|
|
|
|
|
|
|
|
|
|
2016-12-11 14:08:58 +00:00
|
|
|
|
public TapetiConnection(IConfig config)
|
2016-11-21 19:54:29 +00:00
|
|
|
|
{
|
2016-12-11 14:08:58 +00:00
|
|
|
|
this.config = config;
|
2017-01-31 11:01:08 +00:00
|
|
|
|
(config.DependencyResolver as IDependencyContainer)?.RegisterPublisher(GetPublisher);
|
2016-12-11 14:08:58 +00:00
|
|
|
|
|
|
|
|
|
worker = new Lazy<TapetiWorker>(() => new TapetiWorker(config.DependencyResolver, config.MessageMiddleware)
|
2016-11-21 19:54:29 +00:00
|
|
|
|
{
|
|
|
|
|
ConnectionParams = Params ?? new TapetiConnectionParams(),
|
2016-12-11 14:08:58 +00:00
|
|
|
|
Exchange = config.Exchange
|
2016-11-21 19:54:29 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
2016-11-16 22:11:05 +00:00
|
|
|
|
|
|
|
|
|
|
2016-11-20 13:34:50 +00:00
|
|
|
|
public async Task<ISubscriber> Subscribe()
|
2016-11-16 22:11:05 +00:00
|
|
|
|
{
|
2016-12-05 07:00:09 +00:00
|
|
|
|
var subscriber = new TapetiSubscriber(() => worker.Value);
|
2016-12-11 14:08:58 +00:00
|
|
|
|
await subscriber.BindQueues(config.Queues);
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|