64 lines
1.9 KiB
C#
64 lines
1.9 KiB
C#
using System;
|
|
using Confluent.Kafka;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using xEventService.Configuration;
|
|
using xEventService.Constants;
|
|
|
|
namespace xEventService.Providers
|
|
{
|
|
/// <summary>
|
|
/// an Abstract Kafka Consumer Service ...
|
|
/// </summary>
|
|
public abstract class XKafkaConsumerServiceBase : BackgroundService
|
|
{
|
|
/// <summary>
|
|
/// Consumed Topic Messages ...
|
|
/// </summary>
|
|
private readonly string topic;
|
|
|
|
/// <summary>
|
|
/// Kafka Consumer Class ...
|
|
/// </summary>
|
|
public readonly IConsumer<Ignore, string> consumer;
|
|
|
|
private readonly XEventServiceConfiguration configuration;
|
|
|
|
private readonly ILogger<XKafkaConsumerServiceBase> logger;
|
|
|
|
protected XKafkaConsumerServiceBase(
|
|
XEventServiceConfiguration configuration,
|
|
ILogger<XKafkaConsumerServiceBase> logger,
|
|
string topic = XEventServiceConstants.XDefaultTopic
|
|
)
|
|
{
|
|
//
|
|
this.topic = topic;
|
|
this.logger = logger;
|
|
this.configuration = configuration;
|
|
|
|
//
|
|
// Prepare Kafka Consumer Configuration ...
|
|
var config = new ConsumerConfig
|
|
{
|
|
EnableAutoCommit = false,
|
|
BootstrapServers = configuration.Url,
|
|
AutoOffsetReset = AutoOffsetReset.Earliest,
|
|
GroupId = XEventServiceConstants.XDefaultConsumerGroup,
|
|
};
|
|
consumer = new ConsumerBuilder<Ignore, string>(config).Build();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Dispose Implementation ...
|
|
/// </summary>
|
|
public override void Dispose()
|
|
{
|
|
//
|
|
consumer.Close();
|
|
consumer.Dispose();
|
|
GC.SuppressFinalize(this);
|
|
base.Dispose();
|
|
}
|
|
}
|
|
} |