I am working on kafka producer application in which I want to implement the retry functionality. Following is the config that I am using,
@Bean
public ProducerFactory<String, String> producerFactory() {
return new DefaultKafkaProducerFactory<String,String>(senderProps());
}
private Map<String, Object> senderProps() {
Map<String, Object> props = new HashMap<>();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaServerUrl);
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.RETRIES_CONFIG, 1);//number of retries
props.put(ProducerConfig.RETRY_BACKOFF_MS_CONFIG,1000);
return props;
}
@Bean
public KafkaTemplate<String, String> kafkaTemplate(ProducerFactory<String, String> producerFactory) {
return new KafkaTemplate<String, String>(producerFactory);
}
I am using
kafkaTemplate.send("topic",data)
to send to kafka topic.
But it seems the retries are not happening as I expected. I tested it with shutting down the kafka server/broker while producing the record. And after 60 seconds I started the kafka server. But kafka producer retried sending data to server even after 60 seconds. My expectation was, kafka producer will retry for one time and then stop retrying furthur. But it is not happening as I expected. Am I missing anything here? One observation is that I saw one timeout exception at the end which states that-
org.apache.kafka.common.errors.TimeoutException: Expiring 1 record(s) for rcsapi-0:120007 ms has passed since batch creation