-
-
Notifications
You must be signed in to change notification settings - Fork 630
Expand file tree
/
Copy pathMQConsumerBase.cs
More file actions
51 lines (43 loc) · 1.54 KB
/
MQConsumerBase.cs
File metadata and controls
51 lines (43 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using Microsoft.Extensions.Logging;
namespace BotSharp.Abstraction.Infrastructures.MessageQueues;
/// <summary>
/// Abstract base class for RabbitMQ consumers.
/// Implements IMQConsumer to allow other projects to define consumers independently of RabbitMQ.
/// The RabbitMQ-specific infrastructure is handled by RabbitMQService.
/// </summary>
public abstract class MQConsumerBase : IMQConsumer
{
protected readonly IServiceProvider _services;
protected readonly ILogger _logger;
private bool _disposed = false;
/// <summary>
/// Gets the consumer config for this consumer.
/// Override this property to customize exchange, queue and routing configuration.
/// </summary>
public abstract object Config { get; }
protected MQConsumerBase(
IServiceProvider services,
ILogger logger)
{
_services = services;
_logger = logger;
}
/// <summary>
/// Handles the received message from the queue.
/// </summary>
/// <param name="channel">The consumer channel identifier</param>
/// <param name="data">The message data as string</param>
/// <returns>True if the message was handled successfully, false otherwise</returns>
public abstract Task<bool> HandleMessageAsync(string channel, string data);
public void Dispose()
{
if (_disposed)
{
return;
}
var consumerName = GetType().Name;
_logger.LogWarning($"Disposing consumer: {consumerName}");
_disposed = true;
GC.SuppressFinalize(this);
}
}