we are about to write an application to send kafka events
- after startup, this application will run for hours/days/weeks
- depending on the load we will send hunderds of events a minute but there also will be periods, where we will send no events for hours
is it ok to create a producer while startup and use the same producer all the time? in other words: is it guaranteed that the kafka producer can reconnect after every exception (i.E. after network or broker problems, once the resources is available again) (case A) or are there exceptions where the application has to close the producer an create a new producer (case B)?
case a (shown simplied):
while (true) {
try {
ProducerRecord<String, String> record = new ProducerRecord<String, String>(topic, key, value);
producer.send(record).get(); // producer always can re-establish the connection in case of a prior exception
} catch (Exception ex) {
ex.printStackTrace();
}
}
case b (shown simplied):
while (true) {
try {
ProducerRecord<String, String> record = new ProducerRecord<String, String>(topic, key, value);
producer.send(record).get();
} catch (ExceptionAbc? ex) {
// application has to re-create the producer in some cases
producer = new KafkaProducer<String, String>(props);
} catch (Exception ex) {
ex.printStackTrace();
}
}
Thank you for your answer!