2021-11-28 13:18:21 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using PettingZoo.Core.Connection;
|
|
|
|
|
using RabbitMQ.Client;
|
2022-01-30 10:12:35 +00:00
|
|
|
|
using IConnection = RabbitMQ.Client.IConnection;
|
2021-11-28 13:18:21 +00:00
|
|
|
|
|
|
|
|
|
namespace PettingZoo.RabbitMQ
|
|
|
|
|
{
|
|
|
|
|
public class RabbitMQClientConnection : Core.Connection.IConnection
|
|
|
|
|
{
|
2022-01-30 10:12:35 +00:00
|
|
|
|
public Guid ConnectionId { get; } = Guid.NewGuid();
|
|
|
|
|
public ConnectionParams? ConnectionParams { get; }
|
|
|
|
|
public ConnectionStatus Status { get; set; }
|
|
|
|
|
public event EventHandler<StatusChangedEventArgs>? StatusChanged;
|
|
|
|
|
|
|
|
|
|
|
2021-11-28 13:18:21 +00:00
|
|
|
|
private const int ConnectRetryDelay = 5000;
|
|
|
|
|
|
|
|
|
|
private readonly CancellationTokenSource connectionTaskToken = new();
|
2022-01-30 10:12:35 +00:00
|
|
|
|
private Task? connectionTask;
|
2021-11-28 13:18:21 +00:00
|
|
|
|
private readonly object connectionLock = new();
|
2022-01-30 10:12:35 +00:00
|
|
|
|
private IConnection? connection;
|
2021-11-28 13:18:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public RabbitMQClientConnection(ConnectionParams connectionParams)
|
|
|
|
|
{
|
2022-01-30 10:12:35 +00:00
|
|
|
|
ConnectionParams = connectionParams;
|
2021-11-28 13:18:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async ValueTask DisposeAsync()
|
|
|
|
|
{
|
2022-01-30 10:12:35 +00:00
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
if (connectionTask == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-11-28 13:18:21 +00:00
|
|
|
|
connectionTaskToken.Cancel();
|
|
|
|
|
if (!connectionTask.IsCompleted)
|
|
|
|
|
await connectionTask;
|
|
|
|
|
|
|
|
|
|
lock (connectionLock)
|
|
|
|
|
{
|
|
|
|
|
if (connection != null)
|
|
|
|
|
{
|
|
|
|
|
connection.Dispose();
|
|
|
|
|
connection = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-30 10:12:35 +00:00
|
|
|
|
}
|
2021-12-18 11:18:35 +00:00
|
|
|
|
|
2022-01-30 10:12:35 +00:00
|
|
|
|
|
|
|
|
|
public void Connect()
|
|
|
|
|
{
|
|
|
|
|
connectionTask = Task.Factory.StartNew(() => TryConnection(ConnectionParams!, connectionTaskToken.Token), CancellationToken.None);
|
2021-11-28 13:18:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ISubscriber Subscribe(string exchange, string routingKey)
|
2021-12-18 11:18:35 +00:00
|
|
|
|
{
|
|
|
|
|
return CreateSubscriber(exchange, routingKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ISubscriber Subscribe()
|
|
|
|
|
{
|
|
|
|
|
return CreateSubscriber(null, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private ISubscriber CreateSubscriber(string? exchange, string? routingKey)
|
2021-11-28 13:18:21 +00:00
|
|
|
|
{
|
|
|
|
|
lock (connectionLock)
|
|
|
|
|
{
|
2022-01-30 10:12:35 +00:00
|
|
|
|
var model = connection?.CreateModel();
|
2021-11-28 13:18:21 +00:00
|
|
|
|
var subscriber = new RabbitMQClientSubscriber(model, exchange, routingKey);
|
2021-12-18 11:18:35 +00:00
|
|
|
|
if (model != null)
|
2021-11-28 13:18:21 +00:00
|
|
|
|
return subscriber;
|
|
|
|
|
|
2021-12-18 11:18:35 +00:00
|
|
|
|
|
2021-11-28 13:18:21 +00:00
|
|
|
|
void ConnectSubscriber(object? sender, StatusChangedEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
if (args.Status != ConnectionStatus.Connected)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
lock (connectionLock)
|
|
|
|
|
{
|
2022-01-30 10:12:35 +00:00
|
|
|
|
if (connection == null)
|
2021-11-28 13:18:21 +00:00
|
|
|
|
return;
|
2021-12-18 11:18:35 +00:00
|
|
|
|
|
2022-01-30 10:12:35 +00:00
|
|
|
|
subscriber.Connected(connection.CreateModel());
|
2021-11-28 13:18:21 +00:00
|
|
|
|
}
|
2021-12-18 11:18:35 +00:00
|
|
|
|
|
2021-11-28 13:18:21 +00:00
|
|
|
|
StatusChanged -= ConnectSubscriber;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-18 11:18:35 +00:00
|
|
|
|
|
2021-11-28 13:18:21 +00:00
|
|
|
|
StatusChanged += ConnectSubscriber;
|
|
|
|
|
return subscriber;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-18 11:18:35 +00:00
|
|
|
|
|
2021-12-06 13:08:29 +00:00
|
|
|
|
public Task Publish(PublishMessageInfo messageInfo)
|
2021-11-28 13:18:21 +00:00
|
|
|
|
{
|
2022-01-30 10:12:35 +00:00
|
|
|
|
IConnection? lockedConnection;
|
|
|
|
|
|
|
|
|
|
lock (connectionLock)
|
|
|
|
|
{
|
|
|
|
|
lockedConnection = connection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lockedConnection == null)
|
2021-11-28 13:18:21 +00:00
|
|
|
|
throw new InvalidOperationException("Not connected");
|
|
|
|
|
|
2022-01-30 10:12:35 +00:00
|
|
|
|
using (var model = lockedConnection.CreateModel())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
model.BasicPublish(messageInfo.Exchange, messageInfo.RoutingKey, false,
|
|
|
|
|
RabbitMQClientPropertiesConverter.Convert(messageInfo.Properties,
|
|
|
|
|
model.CreateBasicProperties()),
|
|
|
|
|
messageInfo.Body);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
model.Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-28 13:18:21 +00:00
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void TryConnection(ConnectionParams connectionParams, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var factory = new ConnectionFactory
|
|
|
|
|
{
|
|
|
|
|
HostName = connectionParams.Host,
|
|
|
|
|
Port = connectionParams.Port,
|
|
|
|
|
VirtualHost = connectionParams.VirtualHost,
|
|
|
|
|
UserName = connectionParams.Username,
|
|
|
|
|
Password = connectionParams.Password
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
while (!cancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
2022-01-30 10:12:35 +00:00
|
|
|
|
DoStatusChanged(ConnectionStatus.Connecting);
|
2021-11-28 13:18:21 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-01-30 10:12:35 +00:00
|
|
|
|
lock (connectionLock)
|
|
|
|
|
{
|
|
|
|
|
connection = factory.CreateConnection();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DoStatusChanged(ConnectionStatus.Connected);
|
2021-11-28 13:18:21 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2022-01-30 10:12:35 +00:00
|
|
|
|
DoStatusChanged(ConnectionStatus.Error, e);
|
2021-11-28 13:18:21 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Task.Delay(ConnectRetryDelay, cancellationToken).Wait(cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-01-30 10:12:35 +00:00
|
|
|
|
private void DoStatusChanged(ConnectionStatus status, Exception? exception = null)
|
2021-11-28 13:18:21 +00:00
|
|
|
|
{
|
2022-01-30 10:12:35 +00:00
|
|
|
|
Status = status;
|
|
|
|
|
StatusChanged?.Invoke(this, new StatusChangedEventArgs(ConnectionId, status, ConnectionParams, exception));
|
2021-11-28 13:18:21 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|