First pass at producer
This commit is contained in:
18
producer/Model/DataModel.cs
Normal file
18
producer/Model/DataModel.cs
Normal 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
37
producer/Producer.cs
Normal 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
14
producer/producer.csproj
Normal 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>
|
Reference in New Issue
Block a user