0

If I have an existing producer, how can I make it not to create a new one but to use the existing producer?

    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
                    {
                        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;
                    }
                }
        
            return null;
        }
      }  
    }

Since a new producer is created for each api request in the current structure, I cannot read the incoming messages sequentially.

enter image description here

OneCricketeer
  • 151,199
  • 17
  • 111
  • 216
Gülsen Keskin
  • 358
  • 1
  • 11
  • _cannot read the incoming messages sequentially_ - From a consumer? I don't think this has anything to do with how your producer is constructed. Also, if the broker crashes between API requests, the producer would never be re-created, so having one per request would be preferred (and they are cheap to create, and thread-safe) – OneCricketeer Jan 07 '22 at 16:58

0 Answers0