3

If we start the KafkaStream app in the background (say Linux), is there a way to signal from external, to the app, that can initiate the graceful shutdown?

Raman
  • 665
  • 1
  • 15
  • 38

1 Answers1

7

As describe in the docs (https://kafka.apache.org/11/documentation/streams/tutorial), it's recommended to register a shutdown hook that calls KafkaStreams#close() for a clean shutdown:

final CountDownLatch latch = new CountDownLatch(1);

// attach shutdown handler to catch control-c
Runtime.getRuntime().addShutdownHook(new Thread("streams-shutdown-hook") {
    @Override
    public void run() {
        streams.close();
        latch.countDown();
    }
});

try {
    streams.start();
    latch.await();
} catch (Throwable e) {
    System.exit(1);
}
System.exit(0);
Matthias J. Sax
  • 59,682
  • 7
  • 117
  • 137
  • If the app is initiated as a background process, I assume control-c may not be applicable. We can kill the stream app instance using the PID, however not sure if the above would enable a clean shutdown. Please confirm – Raman May 14 '18 at 04:58
  • 2
    `Ctrl-C` sends a `SIGINT` signal, and a normal `kill ` sends a `SIGTERM`. These signals are covered by a shutdown hook (see https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html and https://stackoverflow.com/a/2541618/1743580). What is NOT covered is a `SIGKILL` from a `kill -9 `. I'd recommend to read the Java documentation -- this is not specific to Kafka's Streams API. – miguno May 14 '18 at 10:19
  • Just one addition follow-up, we are on 0.10.1, so by adding a new thread only for the shutdown hook, is there a possibility of facing the same issue as listed in the following? https://stackoverflow.com/questions/42398294/correct-way-to-restart-or-shutdown-the-stream-using-uncaughtexceptionhandler – Raman May 16 '18 at 15:40
  • This should not be an issue. The reported multi-threading issues affect the internal `StreamThreads` only. – Matthias J. Sax May 16 '18 at 16:17