// install signals Signal sig = new Signal("USR2"); Signal.handle(sig, new SignalHandler (){
@Override publicvoidhandle(Signal signalName){ System.out.println(signalName.getName() + " is recevied."); //do some things //也可以另外定义一个类实现SignalHandler接口 } });
Signal sigTERM = new Signal("TERM");/* 注册KILL信号 */ Signal sigINT = new Signal("INT");/* 注册CTRL+C信号 */
发送信号前,需要先通过 ps 或 jps 获取java的进程id,然后运行
1
kill -s SIGUSR2 pid
如果在java的stdout 看到 SIGUSR2 is recevied 字样,说明信号被成功送达了。
sub thread is running sub thread is running sub thread is running sub thread is running clean task1 completed. sub thread is running sub thread is running sub thread is running sub thread is running sub thread is running clean task2 completed
import java.io.File; import java.io.IOException; publicclassShutdownFileTest{ publicstaticvoidmain(String[] args){ // 启动子线程 new Thread() { publicvoidrun(){ while (true) { try { Thread.currentThread().sleep(1000); System.out.println("sub thread is running"); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); //启动shutdownfile监听线程 new Thread() { publicvoidrun(){ File shutDownFile = new File("a.shutdown"); // create shut down file if (!shutDownFile.exists()) { try { shutDownFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } // watch for file deleted then shutdown while (true) { try { if (shutDownFile.exists()) { Thread.currentThread().sleep(1000); } else { System.exit(0); } } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); } }
通过JMX的mbean远程控制来实现
Controlled application: run it with the folowing VM parameters: -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false
ThreadMonitorMBean monitor = new ThreadMonitor(Thread.currentThread());
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
ObjectName name = new ObjectName("com.example:type=ThreadMonitor");
server.registerMBean(monitor, name);
while(!Thread.interrupted()) { // loop until interrupted System.out.println("."); try { Thread.sleep(1000); } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } } } catch(Exception e) { e.printStackTrace(); } finally { // TODO: some final clean up could be here also System.out.println("JMX stopped"); } } }
Controlling application: run it with the stop or start as the command line argument
publicstaticvoidmain(String[] args) { try { // connecting to JMX System.out.println("Connect to JMX service."); JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://:9999/jmxrmi"); JMXConnector jmxc = JMXConnectorFactory.connect(url, null); MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
// Construct proxy for the the MBean object ObjectName mbeanName = new ObjectName("com.example:type=ThreadMonitor"); ThreadMonitorMBean mbeanProxy = JMX.newMBeanProxy(mbsc, mbeanName, ThreadMonitorMBean.class, true);
System.out.println("Connected to: "+mbeanProxy.getName()+", the app is "+(mbeanProxy.isRunning() ? "" : "not ")+"running");