Skip to content

Commit b5c83a0

Browse files
committed
Message Bus Consumers + OutBox Event Publishers
1 parent 9953e5a commit b5c83a0

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

src/Monolith/ClassifiedAds.Application/EventLogs/Commands/PublishEventsCommand.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public async Task HandleAsync(PublishEventsCommand command, CancellationToken ca
4747
{
4848
Id = eventLog.Id.ToString(),
4949
EventType = eventLog.EventType,
50+
EventSource = typeof(PublishEventsCommand).Assembly.GetName().Name,
5051
Payload = eventLog.Message,
5152
};
5253

src/Monolith/ClassifiedAds.BackgroundServer/OutBoxEventPublishers/FileEntryOutBoxEventPublisher.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using ClassifiedAds.Application.FileEntries.DTOs;
1+
using ClassifiedAds.Application.EventLogs.Commands;
2+
using ClassifiedAds.Application.FileEntries.DTOs;
23
using ClassifiedAds.Domain.Constants;
34
using ClassifiedAds.Domain.Entities;
45
using ClassifiedAds.Domain.Infrastructure.MessageBrokers;
@@ -17,6 +18,11 @@ public static string[] CanHandleEventTypes()
1718
return new string[] { EventTypeConstants.FileEntryCreated, EventTypeConstants.FileEntryDeleted };
1819
}
1920

21+
public static string CanHandleEventSource()
22+
{
23+
return typeof(PublishEventsCommand).Assembly.GetName().Name;
24+
}
25+
2026
public FileEntryOutBoxEventPublisher(IMessageBus messageBus)
2127
{
2228
_messageBus = messageBus;

src/Monolith/ClassifiedAds.Domain/Infrastructure/MessageBrokers/IOutBoxEventPublisher.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ public interface IOutBoxEventPublisher
77
{
88
static abstract string[] CanHandleEventTypes();
99

10+
static abstract string CanHandleEventSource();
11+
1012
Task HandleAsync(PublishingOutBoxEvent outbox, CancellationToken cancellationToken = default);
1113
}
1214

@@ -16,5 +18,7 @@ public class PublishingOutBoxEvent
1618

1719
public string EventType { get; set; }
1820

21+
public string EventSource { get; set; }
22+
1923
public string Payload { get; set; }
2024
}

src/Monolith/ClassifiedAds.Domain/Infrastructure/MessageBrokers/MessageBus.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class MessageBus : IMessageBus
1313
{
1414
private readonly IServiceProvider _serviceProvider;
1515
private static List<Type> _consumers = new List<Type>();
16-
private static Dictionary<string, List<Type>> _outboxEventHandlers = new ();
16+
private static Dictionary<string, List<Type>> _outboxEventHandlers = new();
1717

1818
internal static void AddConsumers(Assembly assembly, IServiceCollection services)
1919
{
@@ -43,15 +43,17 @@ internal static void AddOutboxEventPublishers(Assembly assembly, IServiceCollect
4343
foreach (var item in types)
4444
{
4545
var canHandlerEventTypes = (string[])item.InvokeMember(nameof(IOutBoxEventPublisher.CanHandleEventTypes), BindingFlags.InvokeMethod, null, null, null, CultureInfo.CurrentCulture);
46+
var eventSource = (string)item.InvokeMember(nameof(IOutBoxEventPublisher.CanHandleEventSource), BindingFlags.InvokeMethod, null, null, null, CultureInfo.CurrentCulture);
4647

4748
foreach (var eventType in canHandlerEventTypes)
4849
{
49-
if (!_outboxEventHandlers.ContainsKey(eventType))
50+
var key = eventSource + ":" + eventType;
51+
if (!_outboxEventHandlers.ContainsKey(key))
5052
{
51-
_outboxEventHandlers[eventType] = new List<Type>();
53+
_outboxEventHandlers[key] = new List<Type>();
5254
}
5355

54-
_outboxEventHandlers[eventType].Add(item);
56+
_outboxEventHandlers[key].Add(item);
5557
}
5658
}
5759
}
@@ -97,7 +99,8 @@ await _serviceProvider.GetRequiredService<IMessageReceiver<TConsumer, T>>().Rece
9799

98100
public async Task SendAsync(PublishingOutBoxEvent outbox, CancellationToken cancellationToken = default)
99101
{
100-
var handlerTypes = _outboxEventHandlers.ContainsKey(outbox.EventType) ? _outboxEventHandlers[outbox.EventType] : null;
102+
var key = outbox.EventSource + ":" + outbox.EventType;
103+
var handlerTypes = _outboxEventHandlers.ContainsKey(key) ? _outboxEventHandlers[key] : null;
101104

102105
if (handlerTypes == null)
103106
{

0 commit comments

Comments
 (0)