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
2009-11-11 14:22:22
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
2009-11-11 14:24:55
+1
A:
I'd recommend writing a Thread and adding it as a runtime hook. You can see examples here.
David Berger
2009-11-11 14:23:56
+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
2009-11-11 14:25:29