tags:

views:

64

answers:

4

How to set an acknowledgement (like email or SMS) before JVM shuts down (this is on the server side, not client)?

+4  A: 

You may be able to use the Java Shutdown Hook mechanism for this.

It's explained here: http://java.sun.com/j2se/1.5.0/docs/guide/lang/hook-design.html .

Carl Smotricz
They do mention, however, that it's unsafe, deprecated and unreliable. Use at your own risk, in other words. You would be better off building some handling into the mechanism of your app that shuts it down normally, such as the "window closing" event of the main window for a GUI.
Carl Smotricz
+1  A: 

I'd recommend writing a Thread and adding it as a runtime hook. You can see examples here.

David Berger
+4  A: 

You need to add a 'shutdown hook' to the JVM, as described in the Runtime class:

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html#addShutdownHook%28java.lang.Thread)

If you wanted to print out a message when shutting down, you'd do:

Runtime.getRuntime().addShutdownHook(new Thread() { public void run() {
  System.out.println("Goodbye, world!");
} } );

Obviously, fill in the 'goodbye world' bit with what you want.

AlBlue
+1  A: 

Already accepted answer but is this for server or client side?

Xepoch
This is for server side.
Roman Kagan