I am using Kstreams with SpringBoot Application. I have added given below code to handle shutdown for the streams.
KafkaStreams streams = new KafkaStreams(builder.build(), props);
final CountDownLatch latch = new CountDownLatch(1);
Runtime.getRuntime().addShutdownHook(new Thread("streams-shutdown-hook") {
@Override
public void run() {
streams.close(Duration.ofMillis(30000));
latch.countDown();
}
});
try {
streams.setUncaughtExceptionHandler((Thread t, Throwable e) -> {
logger.error("Stream stopped with uncaught error", e);
if (e.getCause() instanceof OffsetOutOfRangeException)
streams.cleanUp();
System.exit(1);
});
streams.start();
latch.await();
} catch (Throwable e) {
logger.error("Stream stopped with error");
System.exit(1);
}
However, when a used topic is not available in kafka, the shutdown hook is never reached, the application just logs Shutdown complete and latch is stuck on await so the application never quits. How can I do a graceful shutdown in this case?