1 4 package org.jfox.ioc.common; 5 6 import org.jfox.ioc.ComponentContext; 7 import org.jfox.ioc.Registry; 8 import org.jfox.ioc.annotation.Managable; 9 import org.jfox.ioc.ext.ActiveComponent; 10 import org.jfox.ioc.ext.ManagableComponent; 11 12 15 16 public class ShutdownHook extends AbstractService implements ManagableComponent, ActiveComponent{ 17 private Registry registry = null; 18 private Thread shutdownThread = null; 19 20 protected void doInit() throws Exception { 21 shutdownThread = new Thread (this); 22 shutdownThread.setName(getName()); 23 24 } 25 26 protected void doStart() throws Exception { 27 Runtime.getRuntime().addShutdownHook(shutdownThread); 28 } 29 30 protected void doStop() throws Exception { 31 if(!shutdownThread.isAlive()) { 33 Runtime.getRuntime().removeShutdownHook(shutdownThread); 34 } 35 } 36 37 protected void doDestroy() throws Exception { 38 shutdownThread = null; 39 } 40 41 public void run() { 42 logger.info("stopping system."); 43 try { 44 registry.stop(); 45 } 46 catch(Exception e) { 47 e.printStackTrace(); 48 } 49 50 } 51 52 @Managable 53 public void stopSystem() { 54 Thread t = new Thread (this); 55 t.start(); 56 57 try { 58 t.join(); 59 } 62 catch(Exception e){ 63 64 } 65 66 } 67 68 public void setComponentContext(ComponentContext ctx) { 69 super.setComponentContext(ctx); 70 registry = ctx.getRegistry(); 71 } 72 73 public static void main(String [] args) { 74 75 } 76 } 77 78 | Popular Tags |