1
0
mirror of synced 2024-11-05 02:59:16 +00:00
Tapeti/TapetiConnection.cs

56 lines
1.3 KiB
C#
Raw Normal View History

using System;
using System.Threading.Tasks;
2016-12-11 14:08:58 +00:00
using Tapeti.Config;
using Tapeti.Connection;
namespace Tapeti
{
public class TapetiConnection : IDisposable
{
2016-12-11 14:08:58 +00:00
private readonly IConfig config;
public TapetiConnectionParams Params { get; set; }
private readonly Lazy<TapetiWorker> worker;
2016-12-11 14:08:58 +00:00
public TapetiConnection(IConfig config)
{
2016-12-11 14:08:58 +00:00
this.config = config;
(config.DependencyResolver as IDependencyContainer)?.RegisterDefault(GetPublisher);
2016-12-11 14:08:58 +00:00
worker = new Lazy<TapetiWorker>(() => new TapetiWorker(config.DependencyResolver, config.MessageMiddleware)
{
ConnectionParams = Params ?? new TapetiConnectionParams()
});
}
public async Task<ISubscriber> Subscribe()
{
var subscriber = new TapetiSubscriber(() => worker.Value);
2016-12-11 14:08:58 +00:00
await subscriber.BindQueues(config.Queues);
return subscriber;
}
public IPublisher GetPublisher()
{
return new TapetiPublisher(() => worker.Value);
}
public async Task Close()
{
if (worker.IsValueCreated)
await worker.Value.Close();
}
public void Dispose()
{
Close().Wait();
}
}
}