1 18 package org.apache.activemq.util; 19 20 import java.util.concurrent.atomic.AtomicBoolean ; 21 22 import org.apache.activemq.Service; 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 31 public abstract class ServiceSupport implements Service { 32 private static final Log log = LogFactory.getLog(ServiceSupport.class); 33 34 private AtomicBoolean started = new AtomicBoolean (false); 35 private AtomicBoolean stopping = new AtomicBoolean (false); 36 private AtomicBoolean stopped = new AtomicBoolean (false); 37 38 public static void dispose(Service service) { 39 try { 40 service.stop(); 41 } 42 catch (Exception e) { 43 log.debug("Could not stop service: " + service + ". Reason: " + e, e); 44 } 45 } 46 47 public void start() throws Exception { 48 if (started.compareAndSet(false, true)) { 49 doStart(); 50 } 51 } 52 53 public void stop() throws Exception { 54 if (stopped.compareAndSet(false, true)) { 55 stopping.set(true); 56 ServiceStopper stopper = new ServiceStopper(); 57 try { 58 doStop(stopper); 59 } 60 catch (Exception e) { 61 stopper.onException(this, e); 62 } 63 stopped.set(true); 64 started.set(false); 65 stopping.set(false); 66 stopper.throwFirstException(); 67 } 68 } 69 70 73 public boolean isStarted() { 74 return started.get(); 75 } 76 77 80 public boolean isStopping() { 81 return stopping.get(); 82 } 83 84 85 88 public boolean isStopped() { 89 return stopped.get(); 90 } 91 92 protected abstract void doStop(ServiceStopper stopper) throws Exception ; 93 94 protected abstract void doStart() throws Exception ; 95 } 96 | Popular Tags |