1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| import java.io.FileWriter;
import java.io.IOException;
import sun.misc.Signal;
import sun.misc.SignalHandler;
public class SophisticatedShutdownSequence {
private static boolean running = true;
public static void init() {
SignalHandler handler = new SignalHandler () {
public void handle(Signal sig) {
if (running) {
running = false;
try {
FileWriter fw = new FileWriter("c:\\essai.txt",true);
fw.write("Signal : "+sig);
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Signal " + sig);
System.out.println("Shutting down database...");
System.exit(0);
} else {
// only on the second attempt do we exit
System.out.println(" database shutdown interrupted!");
System.exit(0);
}
}
};
Signal.handle(new Signal("INT"), handler);
Signal.handle(new Signal("TERM"), handler);
}
public static void main(String args[]) throws Exception {
init();
Object o = new Object();
synchronized (o) {
o.wait(100000);
}
System.exit(0);
}
} |
Partager