java nginx监控服务程序调度算法实现的代码
将内容过程比较常用的内容备份一下,如下内容是关于java nginx监控服务程序调度算法实现的内容,希望对小伙伴们有较大用途。
package com.wole.monitor;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import org.eclipse.jetty.util.ConcurrentHashSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wole.monitor.dao.ServiceDao;
import com.wole.servicemonitor.util.ServiceUtils;
public class MonitorsManage {
private final static Logger logger = LoggerFactory.getLogger(MonitorsManage.class);
private ServiceDao dao;
private Executor commExecutor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 5, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
private CompletionService<Response> completionService = new ExecutorCompletionService<Response>(commExecutor);
private ConcurrentHashSet<MonitorService> currentSet = new ConcurrentHashSet<MonitorService>();
private Queue<MonitorService> sleepQueue = new PriorityBlockingQueue<MonitorService>();
private Queue<MonitorService> executeQueue = new LinkedBlockingQueue<MonitorService>();
private AtomicBoolean isClose = new AtomicBoolean(false);
private AtomicLong startTime = new AtomicLong(0);
private AtomicLong intervalTime = new AtomicLong(0);
public void close() {
logger.info("closing................");
isClose.compareAndSet(false, true);
}
public void init() {
logger.info("初始化");
}
public void work() {
logger.info("开始工作");
Thread productThread = new Thread(new ProductMonitor(1000));
Thread consumerThread = new Thread(new ConsumerMonitor(1000));
Thread recoverThread = new Thread(new RecoverMonitor(1000));
Thread refreshThread = new Thread(new RefreshMonitorService(60000, dao));
productThread.start();
consumerThread.start();
recoverThread.start();
refreshThread.start();
}
class ProductMonitor implements Runnable {
long sleepTime = 1000;
public ProductMonitor(long sleepTime) {
this.sleepTime = sleepTime;
}
@Override
public void run() {
logger.info("生产者开启工作");
long now = System.currentTimeMillis();
long lastTime = now;
startTime.addAndGet(now);
try {
do {
Thread.sleep(sleepTime);
logger.debug("生产者休息{}ms", sleepTime);
now = System.currentTimeMillis();
intervalTime.addAndGet(now - lastTime);
while (sleepQueue.size() > 0) {
MonitorService service = sleepQueue.peek();
if (service.getCurrentTime() - intervalTime.get() < 1) {
if (!currentSet.contains(service)) {
logger.info("service {} 已被删除,不加入执行队列了", service.toString());
continue;
}
executeQueue.add(service);
} else {
logger.debug("还有{}秒可执行", service.getCurrentTime() - intervalTime.get());
break;
}
}
if (sleepQueue.size() <= 0) {
logger.debug("生产队列为空");
}
lastTime = now;
} while (!isClose.get());
} catch (Exception e) {
logger.error("", e);
}
}
}
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);
}
}
}
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;
}
}
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 {
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);
}
}
}
class RefreshMonitorService implements Runnable {
private long sleepTime = 1000;
private ServiceDao dao;
public RefreshMonitorService(long sleepTime, ServiceDao dao) {
this.sleepTime = sleepTime;
if (sleepTime < 60000) {
logger.warn("刷新加载数据的间隔时间不能太短");
throw new RuntimeException("刷新加载数据的间隔时间不能太短");
}
this.dao = dao;
}
private void firstLoad() {
List<MonitorService> monitorService = dao.getService();
logger.info("加载记录:" + monitorService.size());
for (int j = 0; j < monitorService.size(); j++) {
MonitorService service = monitorService.get(j);
service.setCurrentTime(service.getIntervalTime() + intervalTime.get());
currentSet.add(service);
sleepQueue.add(service);
}
}
@Override
public void run() {
logger.info("读取新的service开启工作");
firstLoad();
try {
do {
logger.info("定时加载新的数据监听者休息{}ms", sleepTime);
Thread.sleep(sleepTime);
logger.info("##########开始执行更新数据############");
List<MonitorService> deleteList = dao.deleteService();
List<MonitorService> addList = dao.incrementalService();
logger.info("删除旧的数据共:{}", deleteList.size());
currentSet.removeAll(deleteList);
logger.info("增加新的数据共:{}", addList.size());
currentSet.addAll(addList);
logger.info("更新后的currentSet size:{}", currentSet.size());
for (MonitorService service : addList) {
service.setCurrentTime(service.getIntervalTime() + intervalTime.get());
sleepQueue.add(service);
}
logger.info("########这一轮更新结束");
} while (!isClose.get());
} catch (Exception e) {
logger.error("", e);
}
}
}
class Response {
public Map<String, String> response;
public MonitorService service;
public boolean isNeedWarn;
public boolean isSucToNotify;
}
public void setDao(ServiceDao dao) {
this.dao = dao;
}
}