-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: sample using wildcard for a topic name (#472)
- Loading branch information
Gui Ferreira
authored
Nov 27, 2023
1 parent
9c57625
commit ec1a7e9
Showing
7 changed files
with
135 additions
and
1 deletion.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
samples/KafkaFlow.Sample.WildcardConsumer/KafkaFlow.Sample.WildcardConsumer.csproj
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\KafkaFlow.Admin\KafkaFlow.Admin.csproj" /> | ||
<ProjectReference Include="..\..\src\KafkaFlow.Compressor.Gzip\KafkaFlow.Compressor.Gzip.csproj" /> | ||
<ProjectReference Include="..\..\src\KafkaFlow.Compressor\KafkaFlow.Compressor.csproj" /> | ||
<ProjectReference Include="..\..\src\KafkaFlow.LogHandler.Console\KafkaFlow.LogHandler.Console.csproj" /> | ||
<ProjectReference Include="..\..\src\KafkaFlow.Microsoft.DependencyInjection\KafkaFlow.Microsoft.DependencyInjection.csproj" /> | ||
<ProjectReference Include="..\..\src\KafkaFlow.Serializer.JsonCore\KafkaFlow.Serializer.JsonCore.csproj" /> | ||
<ProjectReference Include="..\..\src\KafkaFlow.Serializer.ProtobufNet\KafkaFlow.Serializer.ProtobufNet.csproj" /> | ||
<ProjectReference Include="..\..\src\KafkaFlow.Serializer\KafkaFlow.Serializer.csproj" /> | ||
<ProjectReference Include="..\..\src\KafkaFlow.TypedHandler\KafkaFlow.TypedHandler.csproj" /> | ||
<ProjectReference Include="..\..\src\KafkaFlow\KafkaFlow.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.17" /> | ||
</ItemGroup> | ||
|
||
</Project> |
18 changes: 18 additions & 0 deletions
18
samples/KafkaFlow.Sample.WildcardConsumer/PrintConsoleMiddleware.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Text; | ||
using KafkaFlow; | ||
|
||
public class PrintConsoleMiddleware : IMessageMiddleware | ||
{ | ||
public Task Invoke(IMessageContext context, MiddlewareDelegate next) | ||
{ | ||
Console.WriteLine( | ||
"Topic: {0} | Partition: {1} | Offset: {2} | Message: {3}", | ||
context.ConsumerContext.Topic, | ||
context.ConsumerContext.Partition, | ||
context.ConsumerContext.Offset, | ||
Encoding.UTF8.GetString( | ||
(byte[])context.Message.Value)); | ||
|
||
return next(context); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System.Text; | ||
using Confluent.Kafka; | ||
using KafkaFlow; | ||
using KafkaFlow.Producers; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using AutoOffsetReset = KafkaFlow.AutoOffsetReset; | ||
|
||
var services = new ServiceCollection(); | ||
|
||
const string producerName = "RandomProducer"; | ||
const string topicPrefix = "random-topic-"; | ||
|
||
services.AddKafka( | ||
kafka => kafka | ||
.UseConsoleLog() | ||
.AddCluster( | ||
cluster => cluster | ||
.WithBrokers(new[] { "localhost:9092" }) | ||
.AddProducer( | ||
producerName, _ => { }) | ||
.AddConsumer( | ||
consumer => consumer | ||
.Topic($"^{topicPrefix}*") // Any topic starting with `random-topic-*` | ||
.WithGroupId("random-topic-handler") | ||
.WithBufferSize(5) | ||
.WithWorkersCount(3) | ||
.WithAutoOffsetReset(AutoOffsetReset.Earliest) | ||
.WithConsumerConfig(new ConsumerConfig() | ||
{ | ||
TopicMetadataRefreshIntervalMs = 5000 // discover new topics every 5 seconds | ||
}) | ||
.AddMiddlewares( | ||
middlewares => middlewares | ||
.Add<PrintConsoleMiddleware>() | ||
) | ||
) | ||
) | ||
); | ||
|
||
var provider = services.BuildServiceProvider(); | ||
|
||
var bus = provider.CreateKafkaBus(); | ||
|
||
await bus.StartAsync(); | ||
|
||
var producer = provider | ||
.GetRequiredService<IProducerAccessor>() | ||
.GetProducer(producerName); | ||
|
||
while (true) | ||
{ | ||
Console.WriteLine("Type the name of a topic to send a message or 'exit' to quit:"); | ||
|
||
var input = Console.ReadLine(); | ||
|
||
if (input is null) | ||
continue; | ||
|
||
if (input.Equals("exit", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
await bus.StopAsync(); | ||
break; | ||
} | ||
|
||
await producer.ProduceAsync( | ||
$"{topicPrefix}{input}", | ||
Guid.NewGuid().ToString(), | ||
Encoding.UTF8.GetBytes( | ||
$"Message to {input}: {Guid.NewGuid()}")); | ||
} |
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
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
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
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