Results 1 to 2 of 2
  1. #1
    nwboy74 is offline Member
    Join Date
    Oct 2008
    Posts
    45
    Rep Power
    0

    Default [SOLVED] Graceful Exit after being killed

    Is it possible to die gracefully in the event of ctrl-c or kill command? I have a server that has a counter variable that needs to be saved to disk before being shut down. Otherwise, the next time it starts, it will have the wrong count. But if, for some reason, the server receives a kill request, can it be intercepted and perform some IO and alerting connected clients before it dies?

  2. #2
    nwboy74 is offline Member
    Join Date
    Oct 2008
    Posts
    45
    Rep Power
    0

    Default

    Once again I have researched myself and answered my own question. But perhaps the solution will be of some use to someone.

    Shutdown hooks are a Java feature that let you have a piece of Java code run whenever the JVM terminates under one of the following conditions:

    • The program exits normally, such as when the last non-daemon thread exits or when the Runtime.exit() method is invoked.
    • The virtual machine is terminated in response to a user interrupt, such as typing CTRL-C, or a system-wide event, such as user logoff or system shutdown (for example, the JVM receives one of the interrupt signals SIGHUP (Unix Only), SIGINT, or SIGTERM).

    Shutdown hooks will not be run if

    • Runtime.halt() method is called to terminate the JVM. Runtime.halt() is provided to allow a quick shutdown of the JVM.
    • The -Xrs JVM option is specified.
    • The JVM exits abnormally, such as an exception condition or forced abort generated by the JVM software.

    A shutdown hook is a Java class that extends java.lang.Thread and is installed with the Runtime.addShutdownHook() method. Your application may install multiple shutdown hooks. On JVM termination, each shutdown hook will be started and will run concurrently, so the normal rules of thread synchronization apply. You can write shutdown hooks to do anything that a normal thread would do; the JVM will treat them like any other. Generally we write a shutdown hook to do any last-minute tidying, such as flushing memory buffers, closing files, or displaying an exit message. The JVM will always wait until all shutdown hooks have completed before continuing with the rest of the shutdown sequence, so it is important that your shutdown hooks do actually complete.

Similar Threads

  1. System.exit 0 or 1?
    By antgaudi in forum New To Java
    Replies: 3
    Last Post: 11-13-2008, 06:33 PM
  2. [SOLVED] exit code 2
    By antgaudi in forum New To Java
    Replies: 2
    Last Post: 09-21-2008, 10:03 PM
  3. system.exit(..)
    By ramakanta.majhi in forum New To Java
    Replies: 2
    Last Post: 06-14-2008, 01:28 AM
  4. How to exit the program..
    By coco in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 05:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •