|
1 |
| -using QuestionProto = Com.Hus.Cdc.Question; |
2 |
| -using Confluent.Kafka; |
3 |
| -using Confluent.Kafka.SyncOverAsync; |
4 |
| -using Confluent.SchemaRegistry.Serdes; |
5 |
| -using Microsoft.Extensions.Hosting; |
6 |
| -using Nest; |
7 |
| -using System; |
8 |
| -using System.Threading; |
9 |
| -using System.Threading.Tasks; |
10 |
| -using netCoreClient.Models; |
11 |
| - |
12 |
| -namespace netCoreClient.Services |
13 |
| -{ |
14 |
| - public class QuestionConsumerService : BackgroundService |
15 |
| - { |
16 |
| - private readonly string topic; |
17 |
| - private readonly IConsumer<string, QuestionProto> kafkaConsumer; |
18 |
| - |
19 |
| - public QuestionConsumerService() |
20 |
| - { |
21 |
| - topic = "outbox.event.question"; |
22 |
| - var consumerConfig = new ConsumerConfig |
23 |
| - { |
24 |
| - BootstrapServers = "host.docker.internal:9092", |
25 |
| - GroupId = "hus-dotnet-consumer", |
26 |
| - AutoOffsetReset = AutoOffsetReset.Earliest, |
27 |
| - }; |
28 |
| - kafkaConsumer = new ConsumerBuilder<string, QuestionProto>(consumerConfig) |
29 |
| - .SetValueDeserializer(new MyDeserializer()) |
30 |
| - .SetErrorHandler((_, e) => Console.WriteLine($"Consumer Error at SetErrorHandler: {e.Reason}")) |
31 |
| - .Build(); |
32 |
| - } |
33 |
| - |
34 |
| - protected override Task ExecuteAsync(CancellationToken stoppingToken) |
35 |
| - { |
36 |
| - new Thread(() => StartConsumerLoop(stoppingToken)).Start(); |
37 |
| - return Task.CompletedTask; |
38 |
| - } |
39 |
| - |
40 |
| - private void StartConsumerLoop(CancellationToken cancellationToken) |
41 |
| - { |
42 |
| - kafkaConsumer.Subscribe(topic); |
43 |
| - |
44 |
| - while (!cancellationToken.IsCancellationRequested) |
45 |
| - { |
46 |
| - try |
47 |
| - { |
48 |
| - var cr = kafkaConsumer.Consume(cancellationToken); |
49 |
| - Console.WriteLine($"{cr.Message.Key}: {cr.Message.Value.QuestionText}"); |
50 |
| - SaveMessage(cr.Message.Value); |
51 |
| - } |
52 |
| - catch (OperationCanceledException) |
53 |
| - { |
54 |
| - break; |
55 |
| - } |
56 |
| - catch (ConsumeException e) |
57 |
| - { |
58 |
| - Console.WriteLine($"Consume error at try/catch: {e.Error.Reason}"); |
59 |
| - |
60 |
| - if (e.Error.IsFatal) |
61 |
| - { |
62 |
| - break; |
63 |
| - } |
64 |
| - } |
65 |
| - catch (Exception e) |
66 |
| - { |
67 |
| - Console.WriteLine($"Unexpected error: {e}"); |
68 |
| - break; |
69 |
| - } |
70 |
| - } |
71 |
| - } |
72 |
| - |
73 |
| - void SaveMessage(QuestionProto val) |
74 |
| - { |
75 |
| - var question = new Question |
76 |
| - { |
77 |
| - Id = val.Id, |
78 |
| - QuestionText = val.QuestionText, |
79 |
| - PubDate = val.PubDate?.ToDateTime() |
80 |
| - }; |
81 |
| - var settings = new ConnectionSettings().DefaultIndex("question"); |
82 |
| - var client = new ElasticClient(settings); |
83 |
| - client.IndexDocument(question); |
84 |
| - } |
85 |
| - |
86 |
| - public override void Dispose() |
87 |
| - { |
88 |
| - // Commit offsets and leave the group cleanly. |
89 |
| - kafkaConsumer.Close(); |
90 |
| - kafkaConsumer.Dispose(); |
91 |
| - |
92 |
| - base.Dispose(); |
93 |
| - } |
94 |
| - } |
95 |
| -} |
| 1 | +using QuestionProto = Com.Hus.Cdc.Question; |
| 2 | +using Confluent.Kafka; |
| 3 | +using Confluent.Kafka.SyncOverAsync; |
| 4 | +using Confluent.SchemaRegistry.Serdes; |
| 5 | +using Microsoft.Extensions.Hosting; |
| 6 | +using Nest; |
| 7 | +using System; |
| 8 | +using System.Threading; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using netCoreClient.Models; |
| 11 | + |
| 12 | +namespace netCoreClient.Services |
| 13 | +{ |
| 14 | + public class QuestionConsumerService : BackgroundService |
| 15 | + { |
| 16 | + private readonly string topic; |
| 17 | + private readonly IConsumer<string, QuestionProto> kafkaConsumer; |
| 18 | + |
| 19 | + public QuestionConsumerService() |
| 20 | + { |
| 21 | + topic = "outbox.event.question"; |
| 22 | + var consumerConfig = new ConsumerConfig |
| 23 | + { |
| 24 | + BootstrapServers = "host.docker.internal:9092", |
| 25 | + GroupId = "hus-dotnet-consumer", |
| 26 | + AutoOffsetReset = AutoOffsetReset.Earliest, |
| 27 | + }; |
| 28 | + kafkaConsumer = new ConsumerBuilder<string, QuestionProto>(consumerConfig) |
| 29 | + .SetValueDeserializer(new MyDeserializer()) |
| 30 | + .SetErrorHandler((_, e) => Console.WriteLine($"Consumer Error at SetErrorHandler: {e.Reason}")) |
| 31 | + .Build(); |
| 32 | + } |
| 33 | + |
| 34 | + protected override Task ExecuteAsync(CancellationToken stoppingToken) |
| 35 | + { |
| 36 | + new Thread(() => StartConsumerLoop(stoppingToken)).Start(); |
| 37 | + return Task.CompletedTask; |
| 38 | + } |
| 39 | + |
| 40 | + private void StartConsumerLoop(CancellationToken cancellationToken) |
| 41 | + { |
| 42 | + kafkaConsumer.Subscribe(topic); |
| 43 | + |
| 44 | + while (!cancellationToken.IsCancellationRequested) |
| 45 | + { |
| 46 | + try |
| 47 | + { |
| 48 | + var cr = kafkaConsumer.Consume(cancellationToken); |
| 49 | + Console.WriteLine($"{cr.Message.Key}: {cr.Message.Value.QuestionText}"); |
| 50 | + SaveMessage(cr.Message.Value); |
| 51 | + } |
| 52 | + catch (OperationCanceledException) |
| 53 | + { |
| 54 | + break; |
| 55 | + } |
| 56 | + catch (ConsumeException e) |
| 57 | + { |
| 58 | + Console.WriteLine($"Consume error at try/catch: {e.Error.Reason}"); |
| 59 | + |
| 60 | + if (e.Error.IsFatal) |
| 61 | + { |
| 62 | + break; |
| 63 | + } |
| 64 | + } |
| 65 | + catch (Exception e) |
| 66 | + { |
| 67 | + Console.WriteLine($"Unexpected error: {e}"); |
| 68 | + break; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + void SaveMessage(QuestionProto val) |
| 74 | + { |
| 75 | + var question = new Question |
| 76 | + { |
| 77 | + Id = val.Id, |
| 78 | + QuestionText = val.QuestionText, |
| 79 | + PubDate = val.PubDate?.ToDateTime() |
| 80 | + }; |
| 81 | + var uri = new Uri("http://elasticsearch:9200"); |
| 82 | + var settings = new ConnectionSettings(uri).DefaultIndex("question"); |
| 83 | + var client = new ElasticClient(settings); |
| 84 | + client.IndexDocument(question); |
| 85 | + } |
| 86 | + |
| 87 | + public override void Dispose() |
| 88 | + { |
| 89 | + // Commit offsets and leave the group cleanly. |
| 90 | + kafkaConsumer.Close(); |
| 91 | + kafkaConsumer.Dispose(); |
| 92 | + |
| 93 | + base.Dispose(); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments