-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKafkaWriter.cs
37 lines (32 loc) · 1011 Bytes
/
KafkaWriter.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
namespace KafkaTester
{
using System;
using System.Threading;
using Confluent.Kafka;
public class MessageProducer : IDisposable
{
private ProducerConfig producerConfig;
private readonly Producer<string, string> producer;
public MessageProducer()
{
this.producerConfig = new ProducerConfig
{
BootstrapServers = "localhost:29092"
};
this.producer = new Producer<string,string>(this.producerConfig);
}
public void Dispose()
{
this.producer.Dispose();
}
public void ProduceMessage(string topic, string key, string value)
{
var message = new Message<string, string> {
Key = key,
Value = value
};
var result = this.producer.ProduceAsync(topic, message).Result;
Console.WriteLine($"Produced {topic} key {key} at offset {result.Offset} ");
}
}
}