Should I start the default signal handler when I define my own handler?

I am trying to pre-clean when SIGINT sent to my Java application using sun.misc.Signal and sun.misc.SignalHandler .

Appears when I register a handler, the default behavior no longer occurs. But there is a SignalHandler.SIG_DFL field containing the "Default Signal Handler". First of all, is this the behavior of a default handler documented anywhere? Secondly, should I use the following template when implementing my own handlers?

 SignalHandler handler = new SignalHandler() { public void handle(Signal sig) { ... // handle SIGINT SignalHandler.SIG_DFL.handle(sig); } }; 

Edit: I know that the sun.misc package sun.misc not portable, and that shutdown hooks are a more reliable way to deal with the application shutting down. Suffice it to say that I know about these functions, and I need to handle signals directly. As far as I know, sun.misc.Signal is the right way to do this.

+5
source share
2 answers

As pointed out in a related question , you should not call SignalHandler.SIG_DFL in SignalHandler as you are calling SEGV . In fact, the JVM does not use SIG_DFL either (for INT , TERM , and HUP ).

Under the covers, all Java does, when it receives any of these signals, System.exit() called. Therefore, if you want to reproduce the default behavior, just call System.exit() yourself. Obviously, if you do not want to exit in response to a signal, there is no need to call the old / standard signal handler.

Warning: this is undocumented behavior. It is unlikely, but entirely possible, that future releases of Java may change this behavior.

0
source

Answering your question: you do not need (or want) to call the default handler. But using this class is not portable. You must install hookdown:

 Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { //your graceful shutdown procedure here } }); 
-1
source

All Articles