18

I have a standalone application in which I have to prompt the user with an confirm dialog box to save the changes made by him when he tries to shutdown the system by start-->shutdown.

I came to know that by using signalhandlers we can do it.
Can some one help me how to use signal handlers

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
chaithu
  • 564
  • 2
  • 12
  • 25

1 Answers1

39

Update May 2012 (2 and half years later)

Trejkaz comments:

On current versions of Java this signal handling code fails because the "INT" signal is "reserved by the VM or the OS".
Additionally, none of the other valid signal names actually fire when something requests the application to close (I just painstakingly tested all of the ones I could find out about...)
The shutdown hook mostly works but we find that in our case it isn't firing, so the next step is obviously to resort to registering a handler behind the JVM's back

The chapter "Integrating Signal and Exception Handling" of the "Troubleshooting Guide for HotSpot VM" mentions the signals "SIGTERM, SIGINT, SIGHUP" only for Solaris OS and Linux.
Only Exception Handling on Windows are mentioned.


Original answer (Sept 2009)

a ShutdownHook should be able to handle that case

Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
    public void run() {
        // what you want to do
    }
}));

(with caveats)
See also:

as an illustration of simple signal handling:

public class Aaarggh {
  public static void main(String[] args) throws Exception {
    Signal.handle(new Signal("INT"), new SignalHandler () {
      public void handle(Signal sig) {
        System.out.println(
          "Aaarggh, a user is trying to interrupt me!!");
        System.out.println(
          "(throw garlic at user, say `shoo, go away')");
      }
    });
    for(int i=0; i<100; i++) {
      Thread.sleep(1000);
      System.out.print('.');
    }
  }
}
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Cool - but that relies on the sun.misc.* tree, which is now deprecated. And hints as to what's the current class we should use? – zigdon Aug 22 '11 at 19:12
  • 1
    @zigdon: Not sure, actually. Those classes are still there in the jdk or openjdk, http://www.docjar.com/docs/api/sun/misc/SignalHandler.html, and even though they are not part of the supported, public interface (http://www.oracle.com/technetwork/java/faq-sun-packages-142232.html), but other solutions might involve JVMTI or JMX (http://stackoverflow.com/questions/5023520/sending-signals-to-a-running-jvm) – VonC Aug 22 '11 at 20:08
  • 1
    On current versions of Java this signal handling code fails because the "INT" signal is "reserved by the VM or the OS". Additionally, none of the other valid signal names actually fire when something requests the application to close (I just painstakingly tested all of the ones I could find out about...) The shutdown hook mostly works but we find that in our case it isn't firing, so the next step is obviously to resort to registering a handler behind the JVM's back. :) – Hakanai May 01 '12 at 23:25
  • @Trejkaz current version: Java7, I suppose? I have included your comment in the answer with some additional links. – VonC May 02 '12 at 05:51
  • Version 7 is what I was testing, certainly. I didn't want to say 7 in case people assumed it to mean "only version 7", because I haven't tested the latest updates for version 6. – Hakanai May 03 '12 at 04:33
  • 2
    I know I shouldn't, but +1 for garlic string. It made me laugh. – Nick Bolton Jun 22 '12 at 01:25