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.




项目中实现的代码

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);


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值