您的位置:首页 > 运维架构 > Apache

RM源码org.apache.hadoop.service.CompositeService分析

2015-08-20 16:03 567 查看
上一篇blog分析了org.apache.hadoop.service.AbstractService主要功能是状态改变,org.apache.hadoop.service.CompositeService主要功能是进行服务的启停。

代码也比较简洁

private final List<Service> serviceList = new ArrayList<Service>();

关于serviceList操作均作了同步





初始化通过conf

protected void serviceInit(Configuration conf) throws Exception {
  List<Service> services = getServices();
  if (LOG.isDebugEnabled()) {
    LOG.debug(getName() + ": initing services, size=" + services.size());
  }
  for (Service service : services) {
    service.init(conf); //通用一个conf配置文件
  }
  super.serviceInit(conf);
}

serviceStart均调用具体service的start方法

protected void serviceStart() throws Exception {
  List<Service> services = getServices();
  if (LOG.isDebugEnabled()) {
    LOG.debug(getName() + ": starting services, size=" + services.size());
  }
  for (Service service : services) {
    // start the service. If this fails that service
    // will be stopped and an exception raised
    service.start();
  }
  super.serviceStart();
}

停止service操作,最终还是调用的service.stop()

protected void serviceStop() throws Exception {
  //stop all services that were started
  int numOfServicesToStop = serviceList.size();
  if (LOG.isDebugEnabled()) {
    LOG.debug(getName() + ": stopping services, size=" + numOfServicesToStop);
  }
  stop(numOfServicesToStop, STOP_ONLY_STARTED_SERVICES);
  super.serviceStop();
}

 

private void stop(int numOfServicesStarted, boolean stopOnlyStartedServices) {
  // stop in reverse order of start
  Exception firstException = null;
  List<Service> services = getServices();
  for (int i = numOfServicesStarted - 1; i >= 0; i--) {
    Service service = services.get(i);
    if (LOG.isDebugEnabled()) {
      LOG.debug("Stopping service #" + i + ": " + service);
    }
    STATE state = service.getServiceState();
    //depending on the stop police
    if (state == STATE.STARTED   //关闭的关键代码
       || (!stopOnlyStartedServices && state == STATE.INITED)) {
      Exception ex = ServiceOperations.stopQuietly(LOG, service);

      if (ex != null && firstException == null) {
        firstException = ex;
      }
    }
  }
  //after stopping all services, rethrow the first exception raised
  if (firstException != null) {
    throw ServiceStateException.convert(firstException);
  }
}

 

public static Exception stopQuietly(Log log, Service service) {
  try {
    stop(service);
  } catch (Exception e) {
    log.warn("When stopping the service " + service.getName()
             + " : " + e,
             e);
    return e;
  }
  return null;
}

public static void stop(Service service) {
  if (service != null) {
   service.stop();
  }
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: