-
-
Save gdyrrahitis/75cfe1b97eb7b5bb0b290d1d6a698522 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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