1

I want one thread to continuously receive message from kafka and also I want to close executor when I press ctrl+C, but it seems that addShutdownHook(...) method isn't called.

How to make sure it will be called? Thanks very much!

public class wwwwwwww {
    static ExecutorService executor = Executors.newFixedThreadPool(2);
    static {
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                Logger.getGlobal().info("***************destroying");
                executor.shutdownNow();
            }
        });
    }

    public static void main(String[] args) throws Exception {
        executor.submit(new Runnable() {
            @Override
            public void run() {
                Logger.getGlobal().info("If you see this log message, then logging is configured correctly and you should see the shutdown hook message.");
                while (true) {
                    Logger.getGlobal().info("Inside loop");
                    // ConsumerRecords<String, String> records = consumer.poll(100);
                    // for (ConsumerRecord<String, String> record : records) {
                    // System.out.printf("offset = %d, key = %s, value = %s\n", record.offset(), record.key(), record.value());
                    // }
                }
            }
        });
    }
}
Mike
  • 147
  • 1
  • 3
  • 11
  • Possible duplicate of [Java Shutdown hook not run](http://stackoverflow.com/questions/12404712/java-shutdown-hook-not-run) – AxelH Nov 16 '16 at 08:41
  • This code does exactly as assumed on my machine; Is this really only a standalone Java application? How exactly are you executing the code? Are you truly sending a [SIGINT](https://en.wikipedia.org/wiki/Unix_signal#SIGINT) to the JVM? Please provide a [minimal working example](https://stackoverflow.com/help/mcve). – errantlinguist Nov 16 '16 at 08:53
  • @errantlinguist,Thanks for your help,i click the stop button in IDE, there is no any destroying information output. – Mike Nov 16 '16 at 09:09
  • Possible duplicate of [ShutdownHook in eclipse](http://stackoverflow.com/questions/12836551/shutdownhook-in-eclipse) – errantlinguist Nov 16 '16 at 13:15

1 Answers1

1

In response to your comment above, the shutdown hook is not being run because you are killing the process running inside an IDE: This means that your program never receives any SIGINT signal (see a related answer) and so, logically, the shutdown hook never gets executed (see the API documentation for Runtime.addShutdownHook(Thread) for a description of when the hook is run).

TL; DR: Don't test system interface functionality (e.g. catching signals) inside an IDE; Run the program "properly" from the command line.

Community
  • 1
  • 1
errantlinguist
  • 3,658
  • 4
  • 18
  • 41
  • Thanks, I tested it via command line, the result the same as the IDE. – Mike Nov 16 '16 at 09:49
  • Well, I built the code above inside Eclipse, exported it using `File->Export...Runnable Jar file` to `wwwwwwww.jar`, and then ran it using `java -jar wwwwwwww.jar` using bash and the hook worked fine; Are you using Windows? – errantlinguist Nov 16 '16 at 09:55
  • In [the answer I already linked to](http://stackoverflow.com/a/12836649/1391325) it's already been stated that it is not possible to reliably send interrupt signals in Windows. – errantlinguist Nov 16 '16 at 13:14