-
Notifications
You must be signed in to change notification settings - Fork 52
/
Program.cs
235 lines (207 loc) · 8.42 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
namespace Chr.Avro.UnionTypeExample
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Chr.Avro.Abstract;
using Chr.Avro.Confluent;
using Chr.Avro.Serialization;
using Chr.Avro.UnionTypeExample.Infrastructure;
using Chr.Avro.UnionTypeExample.Models;
using global::Confluent.Kafka;
using global::Confluent.Kafka.Admin;
using global::Confluent.Kafka.SyncOverAsync;
using global::Confluent.SchemaRegistry;
internal class Program
{
private const string BootstrapServers = "localhost:9092";
private const string SchemaRegistries = "http://localhost:8081";
private const string Topic = "union-type-example";
private static readonly TimeSpan AssignmentTimeout = TimeSpan.FromSeconds(15);
public static async Task<int> Main()
{
using var registryClient = CreateSchemaRegistryClient();
using var admin = CreateAdmin();
using var consumer = CreateConsumer(registryClient);
using var producer = await CreateProducer(registryClient, AutomaticRegistrationBehavior.Always);
Console.WriteLine($"Creating {Topic}...");
await EnsureTopicExists(admin);
consumer.Subscribe(Topic);
foreach (var message in GetEventSequence())
{
await producer.ProduceAsync(Topic, message);
var result = consumer.Consume();
Console.WriteLine($"Received an {result.Message.Value.Event.GetType().Name} event for {result.Message.Key.OrderId}.");
}
return 0;
}
private static IAdminClient CreateAdmin()
{
return new AdminClientBuilder(
new AdminClientConfig
{
BootstrapServers = BootstrapServers,
})
.Build();
}
private static IConsumer<OrderKey, OrderEventRecord> CreateConsumer(
ISchemaRegistryClient registryClient)
{
var deserializerBuilder = new BinaryDeserializerBuilder(
BinaryDeserializerBuilder.CreateDefaultCaseBuilders()
.Prepend(builder => new OrderEventUnionDeserializerBuilderCase(builder)));
return new ConsumerBuilder<OrderKey, OrderEventRecord>(
new ConsumerConfig
{
AutoOffsetReset = AutoOffsetReset.Earliest,
BootstrapServers = BootstrapServers,
EnableAutoCommit = false,
GroupId = $"union-type-example-{Guid.NewGuid()}",
})
.SetAvroKeyDeserializer(registryClient)
.SetValueDeserializer(new AsyncSchemaRegistryDeserializer<OrderEventRecord>(
registryClient,
deserializerBuilder).AsSyncOverAsync())
.Build();
}
private static async Task<IProducer<OrderKey, OrderEventRecord>> CreateProducer(
ISchemaRegistryClient registryClient,
AutomaticRegistrationBehavior automaticRegistrationBehavior)
{
var schemaBuilder = new SchemaBuilder(
SchemaBuilder.CreateDefaultCaseBuilders()
.Prepend(builder => new OrderEventUnionSchemaBuilderCase(builder)));
using var serializerBuilder = new SchemaRegistrySerializerBuilder(
registryClient,
schemaBuilder,
serializerBuilder: new BinarySerializerBuilder(
BinarySerializerBuilder.CreateDefaultCaseBuilders()
.Prepend(builder => new OrderEventUnionSerializerBuilderCase(builder))));
var producerBuilder = new ProducerBuilder<OrderKey, OrderEventRecord>(
new ProducerConfig
{
BootstrapServers = BootstrapServers,
});
await producerBuilder.SetAvroKeySerializer(
registryClient,
SubjectNameStrategy.Topic.ConstructKeySubjectName(Topic),
automaticRegistrationBehavior);
await producerBuilder.SetAvroValueSerializer(
serializerBuilder,
SubjectNameStrategy.Topic.ConstructValueSubjectName(Topic),
automaticRegistrationBehavior);
return producerBuilder.Build();
}
private static ISchemaRegistryClient CreateSchemaRegistryClient()
{
return new CachedSchemaRegistryClient(
new SchemaRegistryConfig
{
Url = SchemaRegistries,
});
}
private static async Task EnsureTopicExists(IAdminClient admin)
{
var metadata = admin.GetMetadata(Topic, TimeSpan.FromSeconds(15));
if (!metadata.Topics.Any(m => m.Topic == Topic))
{
await admin.CreateTopicsAsync(
new[]
{
new TopicSpecification
{
Name = Topic,
},
});
}
}
private static IEnumerable<Message<OrderKey, OrderEventRecord>> GetEventSequence()
{
var order1Key = new OrderKey { OrderId = Guid.NewGuid() };
var order2Key = new OrderKey { OrderId = Guid.NewGuid() };
var order3Key = new OrderKey { OrderId = Guid.NewGuid() };
var product1Id = Guid.NewGuid();
var product2Id = Guid.NewGuid();
yield return new Message<OrderKey, OrderEventRecord>
{
Key = order1Key,
Value = new OrderEventRecord
{
Timestamp = DateTime.UtcNow,
Event = new OrderCreationEvent
{
LineItems = new[]
{
new OrderLineItem { ProductId = product1Id, Quantity = 1 },
new OrderLineItem { ProductId = product2Id, Quantity = 1 },
},
},
},
};
yield return new Message<OrderKey, OrderEventRecord>
{
Key = order2Key,
Value = new OrderEventRecord
{
Timestamp = DateTime.UtcNow,
Event = new OrderCreationEvent
{
LineItems = new[]
{
new OrderLineItem { ProductId = product2Id, Quantity = 5 },
},
},
},
};
yield return new Message<OrderKey, OrderEventRecord>
{
Key = order1Key,
Value = new OrderEventRecord
{
Timestamp = DateTime.UtcNow,
Event = new OrderLineItemModificationEvent
{
Index = 1,
LineItem = new OrderLineItem { ProductId = product2Id, Quantity = 10 },
},
},
};
yield return new Message<OrderKey, OrderEventRecord>
{
Key = order3Key,
Value = new OrderEventRecord
{
Timestamp = DateTime.UtcNow,
Event = new OrderCreationEvent
{
LineItems = Array.Empty<OrderLineItem>(),
},
},
};
yield return new Message<OrderKey, OrderEventRecord>
{
Key = order1Key,
Value = new OrderEventRecord
{
Timestamp = DateTime.UtcNow,
Event = new OrderLineItemModificationEvent
{
Index = 0,
LineItem = new OrderLineItem { ProductId = product1Id, Quantity = 5 },
},
},
};
yield return new Message<OrderKey, OrderEventRecord>
{
Key = order3Key,
Value = new OrderEventRecord
{
Timestamp = DateTime.UtcNow,
Event = new OrderCancellationEvent { },
},
};
}
}
}