监控服务程序调度算法实现(2)

if (sleepQueue.size() <= 0) {
      logger.debug("生产队列为空");
     }
     lastTime = now;
    } while (!isClose.get());
   } catch (Exception e) {
    logger.error("", e);
   }

}

}

/**
  * 消费者
  *
  * @author yzygenuine
  *
  */
 class ConsumerMonitor implements Runnable {
  long sleepTime = 1000;

public ConsumerMonitor(long sleepTime) {
   this.sleepTime = sleepTime;
   if (sleepTime < 1000) {
    throw new RuntimeException("请配置sleepTime值大一些");
   }
  }

@Override
  public void run() {
   logger.info("消费者开启工作");
   try {
    do {
     Thread.sleep(sleepTime);
     logger.debug("消费者休息{}ms", sleepTime);
     while (executeQueue.size() > 0) {
      final MonitorService service = executeQueue.poll();
      completionService.submit(new ExecuteCallable(service));
     }
     logger.debug("消费队列为空");
    } while (!isClose.get());
   } catch (Exception e) {
    logger.error("", e);
   }
  }

}

/**
  * 执行回调类
  *
  * @author yzygenuine
  *
  */
 class ExecuteCallable implements Callable<Response> {
  final MonitorService service;

public ExecuteCallable(MonitorService service) {
   this.service = service;
  }

@Override
  public Response call() throws Exception {
   logger.debug("执行");
   Map<String, String> r = new HashMap<String, String>();
   Response response = new Response();
   response.service = service;
   response.response = r;
   Monitor m = MonitorFactory.getMonitor(service);
   response.isNeedWarn = m.isNeedWarnging(service, r);
   if (response.isNeedWarn) {
    response.isSucToNotify = m.sendNotify(service, r);
   }
   return response;
  }

}

/**
  * 回收者
  *
  * @author yzygenuine
  *
  */
 class RecoverMonitor implements Runnable {
  private long sleepTime = 1000;

private long count = 0;

public RecoverMonitor(long sleepTime) {
   this.sleepTime = sleepTime;
   if (sleepTime < 1000) {
    throw new RuntimeException("请配置sleepTime值大一些");
   }
  }

@Override
  public void run() {
   logger.info("回收者开启工作");
   try {
    do {
     // Thread.sleep(sleepTime);
     Future<Response> response = completionService.take();
     // 重置后进入休眠队列
     MonitorService s = response.get().service;
     if (!currentSet.contains(s)) {
      logger.info("service {} 已被删除,不回收了", s.toString());
      continue;
     }
     // 当前程序已运动的时间+相对间隔时间=绝对的间隔时间
     s.setCurrentTime(s.getIntervalTime() + intervalTime.get());
     sleepQueue.add(s);
     count++;
     logger.info("回收,当前回收数量:" + count);
    } while (!isClose.get());
   } catch (Exception e) {
    logger.error("", e);
   }
  }
 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/af93b894ce770c1b5203450c5527567d.html