Last active
August 6, 2020 21:37
-
-
Save dcomartin/801244efecf324740109e0fe9c2b7ee2 to your computer and use it in GitHub Desktop.
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
public class KafkaConsumerHostedService : IHostedService | |
{ | |
private readonly ILogger<KafkaConsumerHostedService> _logger; | |
private readonly ClusterClient _cluster; | |
public KafkaConsumerHostedService(ILogger<KafkaConsumerHostedService> logger) | |
{ | |
_logger = logger; | |
_cluster = new ClusterClient(new Configuration | |
{ | |
Seeds = "localhost:9092" | |
}, new ConsoleLogger()); | |
} | |
public Task StartAsync(CancellationToken cancellationToken) | |
{ | |
_cluster.ConsumeFromLatest("demo"); | |
_cluster.MessageReceived += record => | |
{ | |
_logger.LogInformation($"Received: {Encoding.UTF8.GetString(record.Value as byte[])}"); | |
}; | |
return Task.CompletedTask; | |
} | |
public Task StopAsync(CancellationToken cancellationToken) | |
{ | |
_cluster?.Dispose(); | |
return Task.CompletedTask; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment