Skip to content

Instantly share code, notes, and snippets.

@gdyrrahitis
Last active August 3, 2023 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gdyrrahitis/75cfe1b97eb7b5bb0b290d1d6a698522 to your computer and use it in GitHub Desktop.
Save gdyrrahitis/75cfe1b97eb7b5bb0b290d1d6a698522 to your computer and use it in GitHub Desktop.
public abstract class RabbitMqClientBase : IDisposable
{
protected const string VirtualHost = "CUSTOM_HOST";
protected readonly string LoggerExchange = $"{VirtualHost}.LoggerExchange";
protected readonly string LoggerQueue = $"{VirtualHost}.log.message";
protected const string LoggerQueueAndExchangeRoutingKey = "log.message";
protected IModel Channel { get; private set; }
private IConnection _connection;
private readonly ConnectionFactory _connectionFactory;
private readonly ILogger<RabbitMqClientBase> _logger;
protected RabbitMqClientBase(
ConnectionFactory connectionFactory,
ILogger<RabbitMqClientBase> logger)
{
_connectionFactory = connectionFactory;
_logger = logger;
ConnectToRabbitMq();
}
private void ConnectToRabbitMq()
{
if (_connection == null || _connection.IsOpen == false)
{
_connection = _connectionFactory.CreateConnection();
}
if (Channel == null || Channel.IsOpen == false)
{
Channel = _connection.CreateModel();
Channel.ExchangeDeclare(
exchange: LoggerExchange,
type: "direct",
durable: true,
autoDelete: false);
Channel.QueueDeclare(
queue: LoggerQueue,
durable: false,
exclusive: false,
autoDelete: false);
Channel.QueueBind(
queue: LoggerQueue,
exchange: LoggerExchange,
routingKey: LoggerQueueAndExchangeRoutingKey);
}
}
public void Dispose()
{
try
{
Channel?.Close();
Channel?.Dispose();
Channel = null;
_connection?.Close();
_connection?.Dispose();
_connection = null;
}
catch (Exception ex)
{
_logger.LogCritical(ex, "Cannot dispose RabbitMQ channel or connection");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment