|
@ -178,27 +178,46 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ |
|
|
{ |
|
|
{ |
|
|
if (_consumerChannel != null) |
|
|
if (_consumerChannel != null) |
|
|
{ |
|
|
{ |
|
|
var consumer = new EventingBasicConsumer(_consumerChannel); |
|
|
|
|
|
consumer.Received += async (model, ea) => |
|
|
|
|
|
{ |
|
|
|
|
|
var eventName = ea.RoutingKey; |
|
|
|
|
|
var message = Encoding.UTF8.GetString(ea.Body); |
|
|
|
|
|
|
|
|
|
|
|
await ProcessEvent(eventName, message); |
|
|
|
|
|
|
|
|
var consumer = new AsyncEventingBasicConsumer(_consumerChannel); |
|
|
|
|
|
|
|
|
_consumerChannel.BasicAck(ea.DeliveryTag, multiple: false); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
consumer.Received += Consumer_Received; |
|
|
|
|
|
|
|
|
_consumerChannel.BasicConsume(queue: _queueName, |
|
|
|
|
|
autoAck: false, |
|
|
|
|
|
consumer: consumer); |
|
|
|
|
|
|
|
|
_consumerChannel.BasicConsume( |
|
|
|
|
|
queue: _queueName, |
|
|
|
|
|
autoAck: false, |
|
|
|
|
|
consumer: consumer); |
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
|
{ |
|
|
{ |
|
|
_logger.LogError("StartBasicConsume can not call on _consumerChannelCreated == false"); |
|
|
|
|
|
|
|
|
_logger.LogError("StartBasicConsume can't call on _consumerChannel == null"); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private async Task Consumer_Received(object sender, BasicDeliverEventArgs eventArgs) |
|
|
|
|
|
{ |
|
|
|
|
|
var eventName = eventArgs.RoutingKey; |
|
|
|
|
|
var message = Encoding.UTF8.GetString(eventArgs.Body); |
|
|
|
|
|
|
|
|
|
|
|
try |
|
|
|
|
|
{ |
|
|
|
|
|
if (message.ToLowerInvariant().Contains("throw-fake-exception")) |
|
|
|
|
|
{ |
|
|
|
|
|
throw new InvalidOperationException($"Fake exception requested: \"{message}\""); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
await ProcessEvent(eventName, message); |
|
|
|
|
|
} |
|
|
|
|
|
catch (Exception ex) |
|
|
|
|
|
{ |
|
|
|
|
|
_logger.LogWarning(ex, "----- ERROR Processing message \"{Message}\"", message); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Even on exception we take the message off the queue.
|
|
|
|
|
|
// in a REAL WORLD app this should be handled with a Dead Letter Exchange (DLX).
|
|
|
|
|
|
// For more information see: https://www.rabbitmq.com/dlx.html
|
|
|
|
|
|
_consumerChannel.BasicAck(eventArgs.DeliveryTag, multiple: false); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private IModel CreateConsumerChannel() |
|
|
private IModel CreateConsumerChannel() |
|
|
{ |
|
|
{ |
|
|
if (!_persistentConnection.IsConnected) |
|
|
if (!_persistentConnection.IsConnected) |
|
@ -209,7 +228,7 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ |
|
|
var channel = _persistentConnection.CreateModel(); |
|
|
var channel = _persistentConnection.CreateModel(); |
|
|
|
|
|
|
|
|
channel.ExchangeDeclare(exchange: BROKER_NAME, |
|
|
channel.ExchangeDeclare(exchange: BROKER_NAME, |
|
|
type: "direct"); |
|
|
|
|
|
|
|
|
type: "direct"); |
|
|
|
|
|
|
|
|
channel.QueueDeclare(queue: _queueName, |
|
|
channel.QueueDeclare(queue: _queueName, |
|
|
durable: true, |
|
|
durable: true, |
|
|