often imitated, never duplicated
|
saidone.org -
often imitated, never duplicated |
|
Main menu
Home
About Projects
AlfMarkSwamp
Links
ush.itaghers.org saidone@ush gameknot virtualmagister Misc
Unless otherwise specified, all contents of this site are licensed under a Creative Commons Attribution-ShareAlike 3.0 Italy License.
|
How to properly shutdown your Java programs - posted by saidone on Mon, 04 Apr 2011 13:26:22 GMT
When you have your Java program running as a daemon, you may have the necessity to shut it down gently (close open files and connections, waiting other threads to terminate, etc.) when required or when some events happens (and asynchronously).I found mainly 2 ways to do it: trapping signals (usually like in a C program running on unix, for example) or adding a shutdown hook to your thread. The code should be quite self explanatory, however in brief let's say that the first method can trap different signals and act consequently (SIGINT, SIGTERM, SIGHUP, etc.) but uses proprietary sun packages (sun.misc) that you may not find on third party JVMs; the second one can catch every exception thrown or not catched elsewhere, giving also a method for correct shutdowns in case of an unexpected error. Obviously, if you don't have nothing against Sun Oracle JVM, a wise combination of both methods is the best one. Here's the code for the shutdown hook:
public class Hook {
private static void shutdown() {
// manage shutdown
System.out.println("Halting...");
}
public static void main(String[] args) throws InterruptedException {
// manage shutdown
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
// perform shutdown operations
shutdown();
System.out.println("Done!");
}
}));
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
if (e instanceof Error) {
System.out.println(e.getMessage());
e.printStackTrace();
System.exit(99);
}
}
});
while (true) {
System.out.println("Alive...");
// do the work
Thread.sleep(1000);
}
}
}
and the code for signal handling:
import sun.misc.Signal;
import sun.misc.SignalHandler;
public final class Trap {
private synchronized static void handle(Signal signal) {
if (signal.getName().equals("INT")) {
System.out.println("INT signal catched. Shutting down.");
// perform shutdown operations
shutdown();
System.exit(1);
}
}
private static void shutdown() {
// manage shutdown
System.out.println("Halting...");
}
public static void main(String args[]) {
// define signals to handle
Signal.handle(new Signal("INT"), new SignalHandler () {
public void handle(Signal signal) {
Trap.handle(signal);
}
});
while (true) {
System.out.println("Alive...");
// do the work
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
shutdown();
System.out.println("Done!");
System.exit(1);
}
}
}
}
Further suggestions are, as usual, welcome!
Tweet No comments yet. Post a new comment back to home |
[[[[[[[ served by kugelmass ]]]]]]]
I have had a perfectly wonderful evening, but this wasn't it. - Groucho Marx
|
|