First pass at producer

This commit is contained in:
2024-06-14 08:42:51 -05:00
commit d0f1e5bc22
8 changed files with 443 additions and 0 deletions

View File

@ -0,0 +1,18 @@
namespace Model
{
public class DataModel
{
public DataModel(string id)
{
this.id = id;
}
public string id { get; set; }
public string? name { get; set; }
public string? description { get; set; }
public string? notes { get; set; }
}
}

37
producer/Producer.cs Normal file
View File

@ -0,0 +1,37 @@
using System;
using System.Threading.Tasks;
using Confluent.Kafka;
using Model;
class Producer
{
public static async Task Main (string[] args)
{
var config = new ProducerConfig { BootstrapServers = "localhost:9092" };
using (var producerBuilder = new ProducerBuilder<string, DataModel>(config).Build())
{
try
{
var msg = new Message<string, DataModel>
{
Key = System.Guid.NewGuid().ToString(),
Value = new DataModel(System.Guid.NewGuid().ToString())
{
name = "Name",
description = "Description",
notes = "This is a test object"
}
};
var dr = await producerBuilder.ProduceAsync("test-topic", msg);
Console.WriteLine($"Delivered '{dr.Value}' to '{dr.TopicPartitionOffset}'");
}
catch (ProduceException<string, DataModel> e)
{
Console.WriteLine($"Delivery failed: {e.Error.Reason}");
}
}
}
}

14
producer/producer.csproj Normal file
View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Confluent.Kafka" Version="2.4.0" />
</ItemGroup>
</Project>