0

Where and how to implement addShutdownHook in a class, which have no main method? Can this used to kill all the active sockets initialized by that class?

Atmaram
  • 152
  • 3
  • 11
  • Have you looked at: http://stackoverflow.com/questions/8722826/when-do-i-need-to-call-this-method-runtime-getruntime-addshutdownhook It's not really a duplicate but I think you can learn things there? Also: http://hellotojavaworld.blogspot.se/2010/11/runtimeaddshutdownhook.html – Samuel Åslund Oct 09 '14 at 06:44
  • @SamuelÅslund: Thanks for your help.. I have searched for it but didn't got this link.. – Atmaram Oct 10 '14 at 11:37
  • @SamuelÅslund: addShutdownHook doesn't work when we kill the server in linux using Kill -9 command. Any other solution for the same?? – Atmaram Oct 13 '14 at 11:52
  • Nope, the whole point of kill -9 is that the program killed is not allowed to do anything at all before it dies. To learn more about the kill command "signal" and "kill" may be good google keywords – Samuel Åslund Oct 13 '14 at 14:49

3 Answers3

1

This Might work for you,

   public class AddShutdownHookSample {
      public void attachShutDownHook(){
           Runtime.getRuntime().addShutdownHook(new Thread() {
                @Override
                public void run() {
                      System.out.println("Inside Add Shutdown Hook");
                }
           });

           System.out.println("Shut Down Hook Attached.");
      }
   }

And in main Method:

public static void main(String[] args) {
  AddShutdownHookSample sample = new AddShutdownHookSample();
  sample.attachShutDownHook();
  System.out.println("Last instruction of Program....");
  System.exit(0);
 }

Describe whole thing that you are trying to do and show the very exact point where you are having trouble this would be easier for other to help you.

Asis
  • 683
  • 3
  • 23
  • @Atmaram I couldnot got you, do you mean that can we do the same on other static functions. Then the answer is YES. – Asis Oct 10 '14 at 11:24
  • I was saying that if I add this part of the code in static block of our class.. Will it work? – Atmaram Oct 10 '14 at 12:04
  • Done Like this... static { Runtime.getRuntime().addShutdownHook ( new Thread() { public void run() { server.shutdown(); } } ); } – Atmaram Jul 10 '15 at 09:11
0

The following is an example, this may help you

public class RuntimeDemo {

   // a class that extends thread that is to be called when program is exiting
 static class Message extends Thread {

  public void run() {
     System.out.println("Bye.");
  }
}

public static void main(String[] args) {
  try {

     // register Message as shutdown hook
     Runtime.getRuntime().addShutdownHook(new Message());

     // print the state of the program
     System.out.println("Program is starting...");

     // cause thread to sleep for 3 seconds
     System.out.println("Waiting for 3 seconds...");
     Thread.sleep(3000);

     // print that the program is closing 
     System.out.println("Program is closing...");


  } catch (Exception e) {
     e.printStackTrace();
  }
 }
}
Anptk
  • 1,125
  • 2
  • 17
  • 28
0

Done like this...

 static { 
         Runtime.getRuntime().addShutdownHook ( new Thread() { 
               public void run() { 
                   server.shutdown(); 
               } 
          } ); 
  }
Atmaram
  • 152
  • 3
  • 11