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;
|
|
|
|
|
|
2018-12-19 19:50:56 +00:00
|
|
|
|
// ReSharper disable UnusedMember.Global
|
|
|
|
|
|
2019-08-13 18:30:04 +00:00
|
|
|
|
// TODO more separation from the actual worker / RabbitMQ Client for unit testing purposes
|
|
|
|
|
|
2016-11-16 22:11:05 +00:00
|
|
|
|
namespace Tapeti
|
|
|
|
|
{
|
2019-08-13 18:30:04 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a connection to RabbitMQ based on the provided Tapeti config.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class TapetiConnection : IConnection
|
2016-11-16 22:11:05 +00:00
|
|
|
|
{
|
2019-08-13 18:30:04 +00:00
|
|
|
|
private readonly ITapetiConfig config;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Specifies the hostname and credentials to use when connecting to RabbitMQ.
|
|
|
|
|
/// Defaults to guest on localhost.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// This property must be set before first subscribing or publishing, otherwise it
|
|
|
|
|
/// will use the default connection parameters.
|
|
|
|
|
/// </remarks>
|
2016-11-21 19:54:29 +00:00
|
|
|
|
public TapetiConnectionParams Params { get; set; }
|
|
|
|
|
|
2019-08-15 09:32:39 +00:00
|
|
|
|
private readonly Lazy<ITapetiClient> client;
|
2019-01-25 13:52:09 +00:00
|
|
|
|
private TapetiSubscriber subscriber;
|
2016-11-21 19:54:29 +00:00
|
|
|
|
|
2021-06-02 18:56:10 +00:00
|
|
|
|
private bool disposed;
|
|
|
|
|
|
2019-08-13 18:30:04 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new instance of a TapetiConnection and registers a default IPublisher
|
|
|
|
|
/// in the IoC container as provided in the config.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="config"></param>
|
|
|
|
|
public TapetiConnection(ITapetiConfig config)
|
2016-11-21 19:54:29 +00:00
|
|
|
|
{
|
2016-12-11 14:08:58 +00:00
|
|
|
|
this.config = config;
|
2017-02-05 22:22:34 +00:00
|
|
|
|
(config.DependencyResolver as IDependencyContainer)?.RegisterDefault(GetPublisher);
|
2016-12-11 14:08:58 +00:00
|
|
|
|
|
2019-08-15 09:32:39 +00:00
|
|
|
|
client = new Lazy<ITapetiClient>(() => new TapetiClient(config, Params ?? new TapetiConnectionParams())
|
2016-11-21 19:54:29 +00:00
|
|
|
|
{
|
2017-07-14 10:33:09 +00:00
|
|
|
|
ConnectionEventListener = new ConnectionEventListener(this)
|
2016-11-21 19:54:29 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
2016-11-16 22:11:05 +00:00
|
|
|
|
|
2019-08-13 18:30:04 +00:00
|
|
|
|
/// <inheritdoc />
|
2019-10-10 14:03:12 +00:00
|
|
|
|
public event ConnectedEventHandler Connected;
|
2019-08-13 18:30:04 +00:00
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2019-02-13 11:00:34 +00:00
|
|
|
|
public event DisconnectedEventHandler Disconnected;
|
2019-08-13 18:30:04 +00:00
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2019-10-10 14:03:12 +00:00
|
|
|
|
public event ConnectedEventHandler Reconnected;
|
2016-11-16 22:11:05 +00:00
|
|
|
|
|
2019-02-13 11:00:34 +00:00
|
|
|
|
|
2019-08-13 18:30:04 +00:00
|
|
|
|
/// <inheritdoc />
|
2017-02-12 14:18:12 +00:00
|
|
|
|
public async Task<ISubscriber> Subscribe(bool startConsuming = true)
|
2016-11-16 22:11:05 +00:00
|
|
|
|
{
|
2019-01-25 13:52:09 +00:00
|
|
|
|
if (subscriber == null)
|
|
|
|
|
{
|
2019-08-13 18:30:04 +00:00
|
|
|
|
subscriber = new TapetiSubscriber(() => client.Value, config);
|
|
|
|
|
await subscriber.ApplyBindings();
|
2019-01-25 13:52:09 +00:00
|
|
|
|
}
|
2017-02-12 14:18:12 +00:00
|
|
|
|
|
|
|
|
|
if (startConsuming)
|
|
|
|
|
await subscriber.Resume();
|
2016-11-20 13:34:50 +00:00
|
|
|
|
|
|
|
|
|
return subscriber;
|
2016-11-16 22:11:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-08-13 18:30:04 +00:00
|
|
|
|
/// <inheritdoc />
|
2019-01-25 13:52:09 +00:00
|
|
|
|
public ISubscriber SubscribeSync(bool startConsuming = true)
|
2017-02-11 20:32:03 +00:00
|
|
|
|
{
|
2019-01-25 13:52:09 +00:00
|
|
|
|
return Subscribe(startConsuming).Result;
|
2017-02-11 20:32:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-08-13 18:30:04 +00:00
|
|
|
|
/// <inheritdoc />
|
2016-11-16 22:11:05 +00:00
|
|
|
|
public IPublisher GetPublisher()
|
|
|
|
|
{
|
2019-08-13 18:30:04 +00:00
|
|
|
|
return new TapetiPublisher(config, () => client.Value);
|
2016-11-16 22:11:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-08-13 18:30:04 +00:00
|
|
|
|
/// <inheritdoc />
|
2016-11-16 22:11:05 +00:00
|
|
|
|
public async Task Close()
|
|
|
|
|
{
|
2019-08-13 18:30:04 +00:00
|
|
|
|
if (client.IsValueCreated)
|
|
|
|
|
await client.Value.Close();
|
2016-11-16 22:11:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-08-13 18:30:04 +00:00
|
|
|
|
/// <inheritdoc />
|
2016-11-16 22:11:05 +00:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2021-06-02 18:56:10 +00:00
|
|
|
|
if (!disposed)
|
|
|
|
|
DisposeAsync().GetAwaiter().GetResult();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public async ValueTask DisposeAsync()
|
|
|
|
|
{
|
|
|
|
|
if (disposed)
|
|
|
|
|
return;
|
2019-10-10 14:03:12 +00:00
|
|
|
|
|
2021-06-02 18:56:10 +00:00
|
|
|
|
if (subscriber != null)
|
|
|
|
|
await subscriber.DisposeAsync();
|
|
|
|
|
|
|
|
|
|
await Close();
|
|
|
|
|
disposed = true;
|
2016-11-16 22:11:05 +00:00
|
|
|
|
}
|
2017-07-14 10:33:09 +00:00
|
|
|
|
|
2019-08-13 18:30:04 +00:00
|
|
|
|
|
2021-06-02 18:56:10 +00:00
|
|
|
|
|
2017-07-14 10:33:09 +00:00
|
|
|
|
private class ConnectionEventListener: IConnectionEventListener
|
|
|
|
|
{
|
|
|
|
|
private readonly TapetiConnection owner;
|
|
|
|
|
|
|
|
|
|
internal ConnectionEventListener(TapetiConnection owner)
|
|
|
|
|
{
|
|
|
|
|
this.owner = owner;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-10 14:03:12 +00:00
|
|
|
|
public void Connected(ConnectedEventArgs e)
|
2017-07-14 10:33:09 +00:00
|
|
|
|
{
|
2019-10-10 14:03:12 +00:00
|
|
|
|
owner.OnConnected(e);
|
2017-07-14 10:33:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-13 11:00:34 +00:00
|
|
|
|
public void Disconnected(DisconnectedEventArgs e)
|
2017-07-14 10:33:09 +00:00
|
|
|
|
{
|
2019-02-13 11:00:34 +00:00
|
|
|
|
owner.OnDisconnected(e);
|
2017-07-14 10:33:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-10 14:03:12 +00:00
|
|
|
|
public void Reconnected(ConnectedEventArgs e)
|
2017-07-14 10:33:09 +00:00
|
|
|
|
{
|
2019-10-10 14:03:12 +00:00
|
|
|
|
owner.OnReconnected(e);
|
2017-07-14 10:33:09 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 18:30:04 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called when a connection to RabbitMQ has been established.
|
|
|
|
|
/// </summary>
|
2019-10-10 14:03:12 +00:00
|
|
|
|
protected virtual void OnConnected(ConnectedEventArgs e)
|
2017-07-14 10:33:09 +00:00
|
|
|
|
{
|
2019-08-13 18:30:04 +00:00
|
|
|
|
var connectedEvent = Connected;
|
2021-10-29 13:47:48 +00:00
|
|
|
|
if (connectedEvent != null)
|
|
|
|
|
Task.Run(() => connectedEvent.Invoke(this, e));
|
2017-07-14 10:33:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 18:30:04 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called when the connection to RabbitMQ has been lost.
|
|
|
|
|
/// </summary>
|
2019-10-10 14:03:12 +00:00
|
|
|
|
protected virtual void OnReconnected(ConnectedEventArgs e)
|
2017-07-14 10:33:09 +00:00
|
|
|
|
{
|
2019-10-10 14:03:12 +00:00
|
|
|
|
subscriber?.Reconnect();
|
2019-09-10 17:38:59 +00:00
|
|
|
|
|
2021-10-29 13:47:48 +00:00
|
|
|
|
var reconnectedEvent = Reconnected;
|
|
|
|
|
if (reconnectedEvent != null)
|
2022-02-09 08:19:56 +00:00
|
|
|
|
Task.Run(() => reconnectedEvent.Invoke(this, e));
|
2017-07-14 10:33:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 18:30:04 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called when the connection to RabbitMQ has been recovered after an unexpected disconnect.
|
|
|
|
|
/// </summary>
|
2019-02-13 11:00:34 +00:00
|
|
|
|
protected virtual void OnDisconnected(DisconnectedEventArgs e)
|
2017-07-14 10:33:09 +00:00
|
|
|
|
{
|
2019-10-10 14:03:12 +00:00
|
|
|
|
subscriber?.Disconnect();
|
|
|
|
|
|
2021-10-29 13:47:48 +00:00
|
|
|
|
var disconnectedEvent = Disconnected;
|
|
|
|
|
if (disconnectedEvent != null)
|
|
|
|
|
Task.Run(() => disconnectedEvent.Invoke(this, e));
|
2017-07-14 10:33:09 +00:00
|
|
|
|
}
|
2016-11-16 22:11:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|