SignalHandler

In my earlier article, I talked about how to do a graceful shutdown your of Java applicationwhen Ctr-C, or the termination signal is intercepted. Now I am going to roughly show you how to do it using Java signal handling.

JVM signal handling is slightly different across different platforms, e.g. across Linux, HP-UX and Windows. So the signal handling program you write in Windows may not work in Linux or HP-UX. Unlike the previous article which is using shutdown hook, which is platform independent, if you want to use signal handling in Java, you have to know which signal(s) you want to handle.

E.g. for Unix/Linux based platform the signals are SEGV, ILL, FPE, BUS, SYS, CPU, FSZ, ABRT, INT, TERM, HUP, USR1, QUIT, BREAK, TRAP, PIPE.

For Windows based platform, the signals are SEGV, ILL, FPE, ABRT, INT, TERM, BREAK.

具体的信号列表可以看如下链接:

http://blog.csdn.net/shuhuai007/article/details/8540663

However, to really determine which signal can be handled in your target platform, you have to test it yourself.

Here is a simple program that you can use to test out signal handling in Java.

   
   
     
     
     
     
[java] view plain copy
  1. import sun.misc.Signal;  
  2. import sun.misc.SignalHandler;  
  3.   
  4. public class SignalHandlerExample implements SignalHandler {  
  5.   
  6.     private SignalHandler oldHandler;  
  7.   
  8.     public static SignalHandler install(String signalName) {  
  9.         Signal diagSignal = new Signal(signalName);  
  10.         SignalHandlerExample instance = new SignalHandlerExample();  
  11.         instance.oldHandler = Signal.handle(diagSignal, instance);  
  12.         return instance;  
  13.     }  
  14.   
  15.     public void handle(Signal signal) {  
  16.         System.out.println("Signal handler called for signal "   
  17.               + signal);  
  18.         try {  
  19.   
  20.             signalAction(signal);  
  21.   
  22.             // Chain back to previous handler, if one exists  
  23.             if (oldHandler != SIG_DFL && oldHandler != SIG_IGN) {  
  24.                 oldHandler.handle(signal);  
  25.             }  
  26.   
  27.         } catch (Exception e) {  
  28.             System.out.println("handle|Signal handler   
  29.                  failed, reason " + e.getMessage());  
  30.             e.printStackTrace();  
  31.         }  
  32.     }  
  33.   
  34.     public void signalAction(Signal signal) {  
  35.         System.out.println("Handling " + signal.getName());  
  36.         System.out.println("Just sleep for 5 seconds.");  
  37.         try {  
  38.             Thread.sleep(5000);  
  39.         } catch (InterruptedException e) {  
  40.             System.out.println("Interrupted: "   
  41.               + e.getMessage());  
  42.         }  
  43.     }  
  44.   
  45.     public static void main(String[] args) {  
  46.         SignalHandlerExample.install("TERM");  
  47.         SignalHandlerExample.install("INT");  
  48.         SignalHandlerExample.install("ABRT");  
  49.   
  50.         System.out.println("Signal handling example.");  
  51.         try {  
  52.             Thread.sleep(50000);  
  53.         } catch (InterruptedException e) {  
  54.             System.out.println("Interrupted: " + e.getMessage());  
  55.         }  
  56.   
  57.     }  
  58. }  

This program handles the TERM, INT, and ABRT signals. When it starts up, it will justsleep. During this time, if you press Ctr-C, the INT signal is triggered and signalAction is called.

Here is the sample output.

C:\>java -cp . SignalHandlerExample
Signal handling example.
Signal handler called for signal SIGINT
Handling INT
Just sleep for 5 seconds.
  • Open Source Library for Java and .NET Interoperability
  • Java: Collecting Profiling Data for Performance Troubleshooting Without Interrupting Your Application
  • Java: Troubleshooting Memory Problem
  • Changing Java Semantics for Null Pointer Exception Handling
  • Java Library for Console Input
  • JTRunner: Another Java Testing Framework




项目中实现的代码

SignalHandler handler = new SignalHandler() {
            public void handle(Signal signal) {
                System.out.println("manual kill ap");
                outputResult(monitorResults);
                System.out.println("write result end");
                System.exit(-1);
            }
        };
        Signal.handle(new Signal("TERM"), handler);
        Signal.handle(new Signal("INT"), handler);


你可能感兴趣的:(java)