I have a message list coming from API, I need to map the elements in this list, send them to the producer and read them one by one. Multiple users can send message lists at the same time. I need to map the lists in order and not read the other list before one list is completely finished. Since a new producer is created for each request in the current structure, I cannot read the incoming messages sequentially. How can I do this by creating a single producer?
public class MailController : BaseController
{
[HttpPost("fatura")]
//[AllowAnonymous]
public bool fatura([FromBody] List<MailMessage> mailMessages)
{
FaturaProducer.Produce(mailMessages);
return default;
}
}
public class FaturaProducer : FProducer<MailMessage>
{
public static Task Produce(List<MailMessage> data)
{
Produce(ConfigurationManager.KafkaSettings.Topics[Topics.FaturaKaydetViaTp].TopicName, data);
return default;
}
}
public class FProducer<T> : ProducerBase2
{
private static readonly ObjectSerializer<T> serializer = new ObjectSerializer<T>();
//public static IProducer<Null, T> producer = new ProducerBuilder<Null, T>(producerConfig).SetValueSerializer(serializer).Build();
public static async Task<DeliveryResult<Null, T>> Produce(string topic,List<T> data)
{
try
{
LogManager.Logger.Debug("Producer initiating for {topic}", topic);
using (var producer = new ProducerBuilder<Null, T>(producerConfig).SetValueSerializer(serializer).Build())
{
LogManager.Logger.Debug("Producer initiated");
LogManager.Logger.Debug("Producing async");
try
{
foreach (var item in data)
{
producer.Poll(TimeSpan.FromSeconds(5));
await producer.ProduceAsync(topic, new Message<Null, T> { Value = item });
}
}
catch (ProduceException<Null, string> ex)
{
LogManager.Logger.Fatal(ex, "Delivery failed: {reason}", ex.Error.Reason);
throw;
}
}
}
catch (Exception ex)
{
LogManager.Logger.Fatal(ex, "Producer failed for {topic}", topic);
}
return null;
}
}