Code re-factorings and formatting in EventBus.RabbitMQ project
This commit is contained in:
parent
086302f5f1
commit
db13fa6091
@ -18,8 +18,7 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
|
|||||||
private readonly int _retryCount;
|
private readonly int _retryCount;
|
||||||
IConnection _connection;
|
IConnection _connection;
|
||||||
bool _disposed;
|
bool _disposed;
|
||||||
|
readonly object sync_root = new object();
|
||||||
object sync_root = new object();
|
|
||||||
|
|
||||||
public DefaultRabbitMQPersistentConnection(IConnectionFactory connectionFactory, ILogger<DefaultRabbitMQPersistentConnection> logger, int retryCount = 5)
|
public DefaultRabbitMQPersistentConnection(IConnectionFactory connectionFactory, ILogger<DefaultRabbitMQPersistentConnection> logger, int retryCount = 5)
|
||||||
{
|
{
|
||||||
@ -28,13 +27,7 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
|
|||||||
_retryCount = retryCount;
|
_retryCount = retryCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsConnected
|
public bool IsConnected => _connection != null && _connection.IsOpen && !_disposed;
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return _connection != null && _connection.IsOpen && !_disposed;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public IModel CreateModel()
|
public IModel CreateModel()
|
||||||
{
|
{
|
||||||
@ -48,7 +41,10 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
|
|||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
if (_disposed) return;
|
if (_disposed)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
_disposed = true;
|
_disposed = true;
|
||||||
|
|
||||||
@ -103,7 +99,10 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
|
|||||||
|
|
||||||
private void OnConnectionBlocked(object sender, ConnectionBlockedEventArgs e)
|
private void OnConnectionBlocked(object sender, ConnectionBlockedEventArgs e)
|
||||||
{
|
{
|
||||||
if (_disposed) return;
|
if (_disposed)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
_logger.LogWarning("A RabbitMQ connection is shutdown. Trying to re-connect...");
|
_logger.LogWarning("A RabbitMQ connection is shutdown. Trying to re-connect...");
|
||||||
|
|
||||||
@ -112,7 +111,10 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
|
|||||||
|
|
||||||
void OnCallbackException(object sender, CallbackExceptionEventArgs e)
|
void OnCallbackException(object sender, CallbackExceptionEventArgs e)
|
||||||
{
|
{
|
||||||
if (_disposed) return;
|
if (_disposed)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
_logger.LogWarning("A RabbitMQ connection throw exception. Trying to re-connect...");
|
_logger.LogWarning("A RabbitMQ connection throw exception. Trying to re-connect...");
|
||||||
|
|
||||||
@ -121,7 +123,10 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
|
|||||||
|
|
||||||
void OnConnectionShutdown(object sender, ShutdownEventArgs reason)
|
void OnConnectionShutdown(object sender, ShutdownEventArgs reason)
|
||||||
{
|
{
|
||||||
if (_disposed) return;
|
if (_disposed)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
_logger.LogWarning("A RabbitMQ connection is on shutdown. Trying to re-connect...");
|
_logger.LogWarning("A RabbitMQ connection is on shutdown. Trying to re-connect...");
|
||||||
|
|
||||||
|
@ -72,7 +72,7 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
|
|||||||
_persistentConnection.TryConnect();
|
_persistentConnection.TryConnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
var policy = RetryPolicy.Handle<BrokerUnreachableException>()
|
var policy = Policy.Handle<BrokerUnreachableException>()
|
||||||
.Or<SocketException>()
|
.Or<SocketException>()
|
||||||
.WaitAndRetry(_retryCount, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, time) =>
|
.WaitAndRetry(_retryCount, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, time) =>
|
||||||
{
|
{
|
||||||
@ -97,7 +97,7 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
|
|||||||
|
|
||||||
channel.BasicPublish(exchange: BROKER_NAME,
|
channel.BasicPublish(exchange: BROKER_NAME,
|
||||||
routingKey: eventName,
|
routingKey: eventName,
|
||||||
mandatory:true,
|
mandatory: true,
|
||||||
basicProperties: properties,
|
basicProperties: properties,
|
||||||
body: body);
|
body: body);
|
||||||
});
|
});
|
||||||
@ -189,7 +189,7 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
|
|||||||
|
|
||||||
await ProcessEvent(eventName, message);
|
await ProcessEvent(eventName, message);
|
||||||
|
|
||||||
channel.BasicAck(ea.DeliveryTag,multiple:false);
|
channel.BasicAck(ea.DeliveryTag, multiple: false);
|
||||||
};
|
};
|
||||||
|
|
||||||
channel.BasicConsume(queue: _queueName,
|
channel.BasicConsume(queue: _queueName,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user