Cucumber/Cloud/Services/ProducerService.cs

34 lines
753 B
C#
Raw Normal View History

2024-11-13 15:24:47 +04:00
using Confluent.Kafka;
namespace Cloud.Services
{
public class ProducerService
{
private readonly IProducer<string, string> _producer;
public ProducerService(IConfiguration configuration)
{
var producerConfig = new ProducerConfig
{
BootstrapServers = configuration["Kafka:BootstrapServers"]
};
//Build the Producer
_producer = new ProducerBuilder<string, string>(producerConfig).Build();
}
//Method for Produce the Message to Kafka Topic
public async Task ProduceMessageAsync(string topic, string value)
{
var kafkaMessage = new Message<string, string>
{
Key = Guid.NewGuid().ToString(),
Value = value
};
//Produce the Message
await _producer.ProduceAsync(topic, kafkaMessage);
}
}
}