I want to exit a spring boot application programmatically when retries are exhausted in order to never miss processing any events. Neither System.exit() nor SpringBootApplication.exit() will terminate the application.
The application is using a @KafkaListener
function that saves events to database and has a SeekToCurrentErrorHandler
with a customer recoverer. The recoverer is where i have tried to exit the application to prevent the kafka offset from being committed.
The application also has a @Scheduled
function, maybe that what is preventing termination??
Googling this leaves me empty. I am not even sure this is the correct way to do this, eg if terminating here will actually prevent offset commit. Here is the code i've tried for configuring recoverer (in kotlin):
fun kafkaListenerContainerFactory(
configurer: ConcurrentKafkaListenerContainerFactoryConfigurer,
kafkaConsumerFactory: ConsumerFactory<Any?, Any?>?
): ConcurrentKafkaListenerContainerFactory<*, *>? {
val factory = ConcurrentKafkaListenerContainerFactory<Any, Any>()
configurer.configure(factory, kafkaConsumerFactory)
factory.setErrorHandler(
SeekToCurrentErrorHandler(
{ _, ex ->
//System.exit(-1)
SpringApplication.exit(appContext, { -1 })
})
)
return factory
}