I have this sample application from here
public class ExampleSignalHandler {
public static void main(String... args) throws InterruptedException {
final long start = System.nanoTime();
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
System.out.format("\nProgram execution took %f seconds\n", (System.nanoTime() - start) / 1e9f);
}
}));
int counter = 0;
while(true) {
System.out.println(counter++);
Thread.sleep(500);
}
}
}
This works just fine, but if I do this in my present application it is not executed on
kill -TERM <pid>
but on
kill -1 <pid>
kill -KILL <pid>
But with those signals I do not have enough time to do my operations before closing the application.
there are a few Threads and a JNI Wrapper used in my application, what can cause the issue that the shutdownHook is not called on kill -TERM?
I need this because my applications uses a network protocol, where I can subscribe to variables. This handles MUST be unsubscribed before closing, otherwise the server run out of memory because of too much handles after a while(Server is not in my control!)