-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Artem Kudriashov
committed
Jan 21, 2022
1 parent
43ee4a8
commit b17f41d
Showing
9 changed files
with
230 additions
and
207 deletions.
There are no files selected for viewing
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
This file was deleted.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System.Threading.Tasks; | ||
using JetBrains.Annotations; | ||
using RabbitMQ.Client; | ||
using RabbitMQ.Client.Events; | ||
|
||
namespace ATI.Services.RabbitMQ.Consumers | ||
{ | ||
[PublicAPI] | ||
public abstract class BaseRmqConsumer : BaseRmqProvider, IRmqConsumer | ||
{ | ||
protected IModel Channel; | ||
private AsyncEventingBasicConsumer _consumer; | ||
protected abstract string QueueName { get; } | ||
protected abstract bool AutoDelete { get; } | ||
protected virtual bool RequeueOnError => false; | ||
protected virtual bool DurableQueue => true; | ||
protected virtual bool AutoAck => true; | ||
protected abstract string RoutingKey { get; } | ||
|
||
public void Init(IConnection connection) | ||
{ | ||
Channel = connection.CreateModel(); | ||
Channel.ExchangeDeclare(exchange: ExchangeName, type: GetExchangeType(), durable: DurableExchange); | ||
Channel.QueueDeclare(queue: QueueName, durable: DurableQueue, exclusive: false, autoDelete: AutoDelete); | ||
Channel.QueueBind(QueueName, ExchangeName, RoutingKey); | ||
|
||
_consumer = new AsyncEventingBasicConsumer(Channel); | ||
_consumer.Received += async (_, args) => await OnReceivedInternalAsync(args).ConfigureAwait(false); | ||
|
||
Channel.BasicConsume(queue: QueueName, autoAck: AutoAck, consumer: _consumer); | ||
|
||
RabbitMqDeclaredQueues.DeclaredQueues.Add(new QueueInfo { QueueName = QueueName }); | ||
} | ||
|
||
protected abstract Task OnReceivedInternalAsync(BasicDeliverEventArgs ea); | ||
public void Dispose() | ||
{ | ||
Channel?.Dispose(); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using ATI.Services.Common.Logging; | ||
using JetBrains.Annotations; | ||
using NLog; | ||
using RabbitMQ.Client.Events; | ||
|
||
namespace ATI.Services.RabbitMQ.Consumers | ||
{ | ||
[PublicAPI] | ||
internal sealed class RawRmqConsumer : BaseRmqConsumer | ||
{ | ||
private readonly ILogger _logger; | ||
private readonly Func<byte[], Task> _onReceivedAsync; | ||
|
||
public RawRmqConsumer( | ||
ILogger logger, | ||
Func<byte[], Task> onReceivedAsync, | ||
ExchangeType exchangeType, | ||
string exchangeName, | ||
string routingKey, | ||
string queueName, | ||
bool autoDelete, | ||
bool durableQueue) | ||
{ | ||
_logger = logger; | ||
_onReceivedAsync = onReceivedAsync; | ||
ExchangeType = exchangeType; | ||
ExchangeName = exchangeName; | ||
QueueName = queueName; | ||
AutoDelete = autoDelete; | ||
RoutingKey = routingKey; | ||
DurableQueue = durableQueue; | ||
} | ||
|
||
protected override ExchangeType ExchangeType { get; } | ||
protected override string ExchangeName { get; } | ||
protected override string QueueName { get; } | ||
protected override bool AutoDelete { get; } | ||
protected override string RoutingKey { get; } | ||
protected override bool DurableQueue { get; } | ||
|
||
protected override async Task OnReceivedInternalAsync(BasicDeliverEventArgs ea) | ||
{ | ||
try | ||
{ | ||
await _onReceivedAsync(ea.Body.ToArray()).ConfigureAwait(false); | ||
if (!AutoAck) | ||
{ | ||
Channel.BasicAck(ea.DeliveryTag, false); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.ErrorWithObject(e, $"Error during message processing {GetType()}", ea); | ||
if (!AutoAck) | ||
{ | ||
Channel.BasicNack(ea.DeliveryTag, false, RequeueOnError); | ||
} | ||
} | ||
} | ||
} | ||
} |
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
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
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
Oops, something went wrong.