-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
10 changed files
with
119 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using KafkaStorm.Registration; | ||
using KafkaStorm.Test.TestConsumers; | ||
|
||
namespace KafkaStorm.Test; | ||
|
||
public class AddingConsumerFromAssemblyTest : TestBase | ||
{ | ||
[Fact] | ||
public void ConsumerShouldHaveBeenAdded() | ||
{ | ||
var result = ConsumerRegistrationFactory.ConsumerConfigs.TryGetValue(typeof(AutomatedConsumer).FullName!, out var config); | ||
Assert.True(result); | ||
} | ||
} |
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,13 @@ | ||
using KafkaStorm.Interfaces; | ||
using KafkaStorm.Test.TestEvents; | ||
|
||
namespace KafkaStorm.Test.TestConsumers; | ||
|
||
public class AutomatedConsumer : IConsumer<AutomatedEvent> | ||
{ | ||
public Task Handle(AutomatedEvent message, CancellationToken cancellationToken) | ||
{ | ||
Console.WriteLine(message.Title); | ||
return Task.CompletedTask; | ||
} | ||
} |
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,12 @@ | ||
using KafkaStorm.Interfaces; | ||
|
||
namespace KafkaStorm.Test.TestEvents; | ||
|
||
public class AutomatedEvent : IMessage | ||
{ | ||
public AutomatedEvent() | ||
{ | ||
Title = "Automated"; | ||
} | ||
public string Title { get; set; } | ||
} |
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,6 @@ | ||
namespace KafkaStorm.Interfaces; | ||
|
||
public interface IMessage | ||
{ | ||
|
||
} |
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
using System; | ||
using KafkaStorm.Exceptions; | ||
|
||
namespace KafkaStorm.Models; | ||
|
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,43 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Confluent.Kafka; | ||
using KafkaStorm.Interfaces; | ||
|
||
namespace KafkaStorm.Registration; | ||
|
||
public static class Extensions | ||
{ | ||
private static Type? GetConsumerType(Assembly assembly, Type messageType) | ||
{ | ||
var iConsumerType = typeof(IConsumer<>).MakeGenericType(messageType); | ||
return assembly.GetTypes().FirstOrDefault(t => | ||
t.GetInterfaces().Contains(iConsumerType) && | ||
t is { IsClass: true, IsVisible: true }); | ||
} | ||
|
||
private static List<Type> GetMessageTypes(Assembly assembly) | ||
{ | ||
return assembly.GetTypes().Where(t => | ||
t.IsClass && | ||
t.GetInterfaces().Contains(typeof(IMessage))) | ||
.ToList(); | ||
} | ||
|
||
public static void AddConsumersFromAssembly(this ConsumerRegistrationFactory crf, Assembly assembly, | ||
ConsumerConfig config) | ||
{ | ||
var messageTypes = GetMessageTypes(assembly); | ||
var method = typeof(ConsumerRegistrationFactory).GetMethod("AddConsumer"); | ||
var methodInfos = (from messageType in messageTypes | ||
let consumerType = GetConsumerType(assembly, messageType) | ||
where consumerType != default | ||
select method!.MakeGenericMethod(consumerType, messageType)) | ||
.ToList(); | ||
foreach (var generic in methodInfos) | ||
{ | ||
generic.Invoke(crf, [config, null]); | ||
} | ||
} | ||
} |
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