automated-testing-demo/producer/Producer.cs

41 lines
1.3 KiB
C#
Raw Normal View History

2024-06-14 13:42:51 +00:00
using System;
2024-06-14 14:48:52 +00:00
using System.Text.Json;
2024-06-14 13:42:51 +00:00
using System.Threading.Tasks;
using Confluent.Kafka;
2024-06-14 14:48:52 +00:00
using Serializers;
2024-06-14 13:42:51 +00:00
using Model;
class Producer
{
public static async Task Main (string[] args)
{
2024-06-14 14:48:52 +00:00
var config = new ProducerConfig { BootstrapServers = "localhost:29092" };
2024-06-14 13:42:51 +00:00
2024-06-14 14:48:52 +00:00
using (var producerBuilder = new ProducerBuilder<string, DataModel>(config)
.SetValueSerializer(new JsonSerializer<DataModel>())
.Build())
2024-06-14 13:42:51 +00:00
{
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}");
}
}
}
}