Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/SlimMessageBus.Host.Kafka/Configs/KafkaClientConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace SlimMessageBus.Host.Kafka;

public class KafkaClientConfig<T>
where T : ClientConfig
{
public T ConfluentConfig { get; set; }

public ILogger Logger { get; set; }
}
42 changes: 21 additions & 21 deletions src/SlimMessageBus.Host.Kafka/Configs/KafkaMessageBusSettings.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace SlimMessageBus.Host.Kafka;
using ConsumerBuilder = Confluent.Kafka.ConsumerBuilder<Confluent.Kafka.Ignore, byte[]>;
using ProducerBuilder = Confluent.Kafka.ProducerBuilder<byte[], byte[]>;

using ConsumerBuilder = Confluent.Kafka.ConsumerBuilder<Confluent.Kafka.Ignore, byte[]>;
using ProducerBuilder = Confluent.Kafka.ProducerBuilder<byte[], byte[]>;

public class KafkaMessageBusSettings
{
/// <summary>
Expand All @@ -12,7 +12,7 @@ public class KafkaMessageBusSettings
/// <summary>
/// Factory method that creates a <see cref="ProducerBuilder"/> based on the supplied settings.
/// </summary>
public Func<ProducerConfig, ProducerBuilder> ProducerBuilderFactory { get; set; }
public Func<KafkaClientConfig<ProducerConfig>, ProducerBuilder> ProducerBuilderFactory { get; set; }
/// <summary>
/// Factory method that created a <see cref="Producer"/>.
/// See also: https://kafka.apache.org/documentation/#producerconfigs
Expand All @@ -21,7 +21,7 @@ public class KafkaMessageBusSettings
/// <summary>
/// Factory method that creates a <see cref="ConsumerBuilder"/> based on the supplied settings and consumer GroupId.
/// </summary>
public Func<ConsumerConfig, ConsumerBuilder> ConsumerBuilderFactory { get; set; }
public Func<KafkaClientConfig<ConsumerConfig>,ConsumerBuilder> ConsumerBuilderFactory { get; set; }
/// <summary>
/// Factory method that creates settings based on the consumer GroupId.
/// See also: https://kafka.apache.org/documentation/#newconsumerconfigs
Expand All @@ -32,30 +32,30 @@ public class KafkaMessageBusSettings
/// </summary>
public TimeSpan ConsumerPollRetryInterval { get; set; }

/// <summary>
/// Serializer used to serialize Kafka message header values. If not specified the default serializer will be used (setup as part of the bus config). By default the <see cref="DefaultKafkaHeaderSerializer"/> is used.
/// <summary>
/// Serializer used to serialize Kafka message header values. If not specified the default serializer will be used (setup as part of the bus config). By default the <see cref="DefaultKafkaHeaderSerializer"/> is used.
/// </summary>
public IMessageSerializerProvider HeaderSerializer { get; set; }
/// <summary>
/// Should the commit on partitions for the consumed messages happen when the bus is stopped (or disposed)?
/// This ensures the message reprocessing is minimized in between application restarts.
/// Default is true.
public IMessageSerializerProvider HeaderSerializer { get; set; }

/// <summary>
/// Should the commit on partitions for the consumed messages happen when the bus is stopped (or disposed)?
/// This ensures the message reprocessing is minimized in between application restarts.
/// Default is true.
/// </summary>
public bool EnableCommitOnBusStop { get; set; } = true;

public KafkaMessageBusSettings()
{
{
ProducerConfig = (config) => { };
ProducerBuilderFactory = (config) => new ProducerBuilder<byte[], byte[]>(config);
ProducerBuilderFactory = (config) => new ProducerBuilder<byte[], byte[]>(config.ConfluentConfig);
ConsumerConfig = (config) => { };
ConsumerBuilderFactory = (config) => new ConsumerBuilder<Ignore, byte[]>(config);
ConsumerPollRetryInterval = TimeSpan.FromSeconds(2);
ConsumerBuilderFactory = (config) => new ConsumerBuilder<Ignore, byte[]>(config.ConfluentConfig);
ConsumerPollRetryInterval = TimeSpan.FromSeconds(2);
HeaderSerializer = new DefaultKafkaHeaderSerializer();
}
}

public KafkaMessageBusSettings(string brokerList) : this()
{
{
BrokerList = brokerList;
}
}
17 changes: 11 additions & 6 deletions src/SlimMessageBus.Host.Kafka/Consumer/KafkaGroupConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,22 @@

protected IConsumer CreateConsumer(string group)
{
var config = new ConsumerConfig
var config = new KafkaClientConfig<ConsumerConfig>
{
GroupId = group,
BootstrapServers = ProviderSettings.BrokerList
ConfluentConfig = new ConsumerConfig
{
GroupId = group,
BootstrapServers = ProviderSettings.BrokerList
},
Logger = Logger
};
ProviderSettings.ConsumerConfig(config);

ProviderSettings.ConsumerConfig(config.ConfluentConfig);

// ToDo: add support for auto commit

Check warning on line 83 in src/SlimMessageBus.Host.Kafka/Consumer/KafkaGroupConsumer.cs

View workflow job for this annotation

GitHub Actions / sonar

Complete the task associated to this 'TODO' comment.
config.EnableAutoCommit = false;
config.ConfluentConfig.EnableAutoCommit = false;
// Notify when we reach EoF, so that we can do a manual commit
config.EnablePartitionEof = true;
config.ConfluentConfig.EnablePartitionEof = true;

var consumer = ProviderSettings.ConsumerBuilderFactory(config)
.SetStatisticsHandler((_, json) => OnStatistics(json))
Expand Down Expand Up @@ -141,7 +146,7 @@
}
}
catch (OperationCanceledException)
{

Check warning on line 149 in src/SlimMessageBus.Host.Kafka/Consumer/KafkaGroupConsumer.cs

View workflow job for this annotation

GitHub Actions / sonar

Either remove or fill this block of code.
}

Logger.LogInformation("Group [{Group}]: Unsubscribing from topics", Group);
Expand Down Expand Up @@ -267,7 +272,7 @@

protected virtual void OnStatistics(string json)
{
Logger.LogTrace("Group [{Group}]: Statistics: {statistics}", Group, json);

Check warning on line 275 in src/SlimMessageBus.Host.Kafka/Consumer/KafkaGroupConsumer.cs

View workflow job for this annotation

GitHub Actions / sonar

Use PascalCase for named placeholders.
}

#region Implementation of IKafkaCommitController
Expand Down
12 changes: 8 additions & 4 deletions src/SlimMessageBus.Host.Kafka/KafkaMessageBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,17 @@
public IProducer CreateProducerInternal()
{
_logger.LogTrace("Creating producer settings");
var config = new ProducerConfig
var config = new KafkaClientConfig<ProducerConfig>
{
BootstrapServers = ProviderSettings.BrokerList,
ConfluentConfig = new ProducerConfig()
{
BootstrapServers = ProviderSettings.BrokerList
},
Logger = _logger
};
ProviderSettings.ProducerConfig(config);
ProviderSettings.ProducerConfig(config.ConfluentConfig);

_producerDeliveryReportsEnabled = config.EnableDeliveryReports ?? true; // when not set per Confluent.KafkaNet docs, it's defaulting to true
_producerDeliveryReportsEnabled = config.ConfluentConfig.EnableDeliveryReports ?? true; // when not set per Confluent.KafkaNet docs, it's defaulting to true

_logger.LogDebug("Producer settings: {ProducerSettings}", config);
return ProviderSettings.ProducerBuilderFactory(config).Build();
Expand Down Expand Up @@ -135,7 +139,7 @@
}
}

public override async Task ProduceToTransport(object message, Type messageType, string path, IDictionary<string, object> messageHeaders, IMessageBusTarget targetBus, CancellationToken cancellationToken)

Check warning on line 142 in src/SlimMessageBus.Host.Kafka/KafkaMessageBus.cs

View workflow job for this annotation

GitHub Actions / sonar

Refactor this method to reduce its Cognitive Complexity from 25 to the 15 allowed.
{
static void OnMessageDelivered(ILogger logger, DeliveryResult<byte[], byte[]> deliveryResult, object message, Type messageType)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public KafkaLoopGroupConsumerTests()
.Setup(builder => builder.Build())
.Returns(mockKafkaConsumer.Object);

var mockConsumerBuilderFactory = new Mock<Func<ConsumerConfig, ConsumerBuilder<Ignore, byte[]>>>();
var mockConsumerBuilderFactory = new Mock<Func<KafkaClientConfig<ConsumerConfig>, ConsumerBuilder<Ignore, byte[]>>>();
mockConsumerBuilderFactory
.Setup(factory => factory(It.IsAny<ConsumerConfig>()))
.Setup(factory => factory(It.IsAny<KafkaClientConfig<ConsumerConfig>>()))
.Returns(mockConsumerBuilder.Object);

var settings = new KafkaMessageBusSettings
Expand Down
Loading