1 6 7 package org.jfox.ioc.common; 8 9 import org.jfox.ioc.annotation.Managable; 10 import org.jfox.ioc.ext.ServicableComponent; 11 12 15 16 public abstract class AbstractService extends AbstractComponent implements ServicableComponent, Runnable { 17 18 19 public AbstractService() { 20 super(); 21 } 22 23 public AbstractService(String name) { 24 super(name); 25 } 26 27 public boolean isStarted() { 28 return state == State.STARTED || state == State.STARTING; 29 } 30 31 @Managable 32 public void start() throws Exception { 33 if(!State.canStart(state)) { 34 logger.warn(name + " can not start, state = " + state); 35 return; 36 } 37 38 logger.info("starting..."); 39 40 state = State.STARTING; 41 42 try { 43 doStart(); 44 } 45 catch(Exception e) { 46 state = State.INTERRUPTED; 47 logger.error("starting failed", e); 48 throw e; 49 } 50 state = State.STARTED; 51 logger.info("started."); 52 } 53 54 @Managable 55 public void stop() throws Exception { 56 if(!State.canStop(state)) { 57 logger.warn(name + " can not stop, state = " + state); 58 return; 59 } 60 61 logger.info("stopping..."); 62 63 state = State.STOPPING; 64 65 try { 66 doStop(); 67 } 68 catch(Exception e) { 69 state = State.INTERRUPTED; 70 logger.error("stopping failed", e); 71 throw e; 72 } 73 state = State.STOPPED; 74 logger.info("stopped"); 75 76 } 77 78 81 protected abstract void doStart() throws Exception ; 82 83 86 protected abstract void doStop() throws Exception ; 87 88 } 89 90 | Popular Tags |