-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Kafka Module: Add Support for Configuring Consumer and Produce…
…r Factories (#6139) * Enable Kafka Worker Factory and Refactor Worker Implementation Introduce a flexible worker factory mechanism allowing custom worker creation with DefaultWorkerFactory as the initial implementation. Enhance Worker class to be generic, remove manual consumer configuration, and streamline message processing logic, improving code maintainability and extensibility. * Refactor Kafka configuration properties Renamed configuration properties in Consumer and Producer entities. Updated references in the codebase to use the new `Config` property instead of `ConsumerConfig` and `BootstrapServers`. Adjusted appsettings.json to match the new configuration schema. * Add Kafka producer and consumer implementation Implemented classes and interfaces to handle Kafka producers and consumers, including `ProducerProxy`, `ConsumerProxy`, and related context classes and factories. Refactored existing code to utilize these new implementations, replacing worker terminology with consumer and addressing context-specific fields. * Remove redundant code in DefaultConsumerFactory and SendMessage Removed commented-out unused return statement in DefaultConsumerFactory. Also eliminated explicit producer.Dispose() call in SendMessage, as the 'using' statement already handles resource cleanup. * Add ExpandoObject producer and consumer factories Replaced DefaultSerializers with new JsonSerializer and JsonDeserializer classes. Introduced ExpandoObjectProducerFactory and ExpandoObjectConsumerFactory to handle dynamic types. Updated workflow and configuration to use the new factories. * Refactor bookmark processing and manage worker subscriptions Refactored bookmark processing logic to utilize extension methods. Optimized worker subscriptions by centralizing topic subscription management and added logging for subscribed topics. This improves maintainability and clarity of the codebase. * Refactor worker creation to use ActivatorUtilities Updated WorkerManager to instantiate workers using ActivatorUtilities for better dependency injection support. This enhances code readability and maintains consistency with the service provider approach used throughout the codebase.
- Loading branch information
1 parent
8b4ff07
commit b534b42
Showing
42 changed files
with
433 additions
and
315 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 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
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
33 changes: 33 additions & 0 deletions
33
src/modules/Elsa.Common/Serialization/TypeTypeConverter.cs
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,33 @@ | ||
using System.ComponentModel; | ||
using System.Globalization; | ||
using JetBrains.Annotations; | ||
|
||
namespace Elsa.Common.Serialization; | ||
|
||
[PublicAPI] | ||
public class TypeTypeConverter : TypeConverter | ||
{ | ||
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) | ||
{ | ||
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); | ||
} | ||
|
||
public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value) | ||
{ | ||
if (value is string stringValue) | ||
return Type.GetType(stringValue); | ||
return base.ConvertFrom(context, culture, value); | ||
} | ||
|
||
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) | ||
{ | ||
return destinationType == typeof(string) || base.CanConvertTo(context, destinationType); | ||
} | ||
|
||
public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType) | ||
{ | ||
if (destinationType == typeof(string) && value is Type type) | ||
return type.AssemblyQualifiedName; | ||
return base.ConvertTo(context, culture, value, destinationType); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Elsa.Kafka; | ||
|
||
public interface IConsumer | ||
{ | ||
object Consumer { get; } | ||
} |
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 Elsa.Kafka; | ||
|
||
public interface IConsumerFactory | ||
{ | ||
IConsumer CreateConsumer(CreateConsumerContext workerContext); | ||
} |
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,8 @@ | ||
using Confluent.Kafka; | ||
|
||
namespace Elsa.Kafka; | ||
|
||
public interface IProducer : IDisposable | ||
{ | ||
Task ProduceAsync(string topic, object value, Headers? headers = null, CancellationToken cancellationToken = default); | ||
} |
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 Elsa.Kafka; | ||
|
||
public interface IProducerFactory | ||
{ | ||
IProducer CreateProducer(CreateProducerContext workerContext); | ||
} |
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
13 changes: 0 additions & 13 deletions
13
src/modules/Elsa.Kafka/Entities/ConsumerConfigDefinition.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Confluent.Kafka; | ||
using Elsa.Common.Entities; | ||
using Elsa.Kafka.Factories; | ||
|
||
namespace Elsa.Kafka; | ||
|
||
public class ConsumerDefinition : Entity | ||
{ | ||
public string Name { get; set; } = default!; | ||
public Type FactoryType { get; set; } = typeof(DefaultConsumerFactory); | ||
public ConsumerConfig Config { get; set; } = new(); | ||
} |
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,9 +1,12 @@ | ||
using Confluent.Kafka; | ||
using Elsa.Common.Entities; | ||
using Elsa.Kafka.Factories; | ||
|
||
namespace Elsa.Kafka; | ||
|
||
public class ProducerDefinition : Entity | ||
{ | ||
public string Name { get; set; } = default!; | ||
public ICollection<string> BootstrapServers { get; set; } = []; | ||
public Type FactoryType { get; set; } = typeof(DefaultProducerFactory); | ||
public ProducerConfig Config { get; set; } = new(); | ||
} |
18 changes: 18 additions & 0 deletions
18
src/modules/Elsa.Kafka/Extensions/ServiceCollectionExtensions.cs
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,18 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Elsa.Kafka; | ||
|
||
public static class ServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddConsumerFactory<T>(this IServiceCollection services) where T : class, IConsumerFactory | ||
{ | ||
services.AddScoped<T>(); | ||
return services; | ||
} | ||
|
||
public static IServiceCollection AddProducerFactory<T>(this IServiceCollection services) where T : class, IProducerFactory | ||
{ | ||
services.AddScoped<T>(); | ||
return services; | ||
} | ||
} |
Oops, something went wrong.