All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.checker.AbstractChecker Maven / Gradle / Ivy

The newest version!
package com.checker;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.utils.CustomLogger;
import com.utils.HttpUtils;
import com.utils.MyException;
import com.utils.ProxyUtils;

@Component
public abstract class AbstractChecker implements CommandLineRunner {
	public Logger common = CustomLogger.getLogger("common.log");
	private final int queuesize = 30;
	private BlockingQueue taskqueue = new LinkedBlockingQueue(queuesize);
	private BlockingQueue resultqueue = new LinkedBlockingQueue(queuesize);
	@Autowired
	protected HttpUtils httpUtils;
	@Autowired
	private DefaultCheckerConf bcc;
	@Autowired
	private ProxyUtils pu;

	String taskurl;
	String resulturl;
	String showurl;

	private String appName = "justtest";
	private String dbName;
	private String collName;
	private String resultColl;
	private String monitors;

	private int taskgetter = 1;
	private int worker = 1;
	private int resultposter = 1;
	private boolean detailedlog = false;
	private int httpRetryTime = 1;
	private int taskPerTime = 10;
	private int resultPerTime = 10;

	private int connecttime = 60000;
	private int readtime = 60000;
	private int type = -1;

	public static enum Result {
		exist, unexist, httpfail, unknow, exception, half, unauth, temp, forbidden, tempforbidden
	}

	public AbstractChecker() {

	}

	public void monitor() {

		Map map = new HashMap();
		map.put("Accept", "application/json");
		map.put("Content-Type", "application/json");
		map.put("Cookie", "XSRF-TOKEN=09ac6089-2c10-4138-a18c-abfa7f6e99c1");
		map.put("X-XSRF-TOKEN", "09ac6089-2c10-4138-a18c-abfa7f6e99c1");
		try {
			JSONObject json = new JSONObject();
			json.put("appName", appName);
			json.put("dbName", dbName);
			json.put("taskName", collName);
			json.put("resultName", resultColl);
			json.put("monitors", monitors);

			common.warn("MonitorPost:" + json.toString());

			JSONObject httpResult = null;
			JSONObject realResult = null;
			int count=0;
			do {
				if (httpResult != null)
					Thread.sleep(1000);
				httpResult = httpUtils.httppost(showurl, json.toString(), map);
				common.warn("MonitorReturn:" + httpResult.toString());
				if(httpResult.getIntValue("ec")==0){
					realResult = httpResult.getJSONObject("result");
				}else{
					Thread.sleep(1000);
				}
				if(count++>3)
					break;
			} while (realResult==null||realResult.getIntValue("ec") == -1);
		} catch (InterruptedException e) {
			throw new MyException(e);
		} catch (Exception e) {
			common.error(e);
		}

	}

	@Override
	public void run(String... arg0) throws Exception {

		taskurl = "http://" + bcc.getAddress() + "/checker/task";
		resulturl = "http://" + bcc.getAddress() + "/checker/result";
		showurl = "http://" + bcc.getAddress() + "/show/justsaveop";
		common.warn("taskurl:" + taskurl);
		common.warn("resulturl:" + resulturl);
		common.warn("showurl:" + showurl);

		System.out.println(monitors);
		common.warn(bcc);
		common.warn(httpUtils);
		common.warn(pu);
		common.warn(this);
		// 初始化相关变量开始
		ConfigurationProperties configurationProperties = this.getClass().getAnnotation(ConfigurationProperties.class);
		String appName = configurationProperties.prefix();
		this.setAppName(appName);
		System.out.println(appName + "被启动!");

		if (this.getConnecttime() != 60000)
			httpUtils.setConnecttime(this.getConnecttime());
		if (this.getReadtime() != 60000)
			httpUtils.setReadtime(this.getReadtime());
		if (this.getType() != -1)
			pu.setType(this.getType());

		if (this.getDbName() == null)
			this.setDbName(bcc.getDbName());
		if (this.getCollName() == null)
			this.setCollName(bcc.getCollName());

		if (bcc.getTaskgetter() != 1 && this.getTaskgetter() == 1)
			this.setTaskgetter(bcc.getTaskgetter());
		if (bcc.getWorker() != 1 && this.getWorker() == 1)
			this.setWorker(bcc.getWorker());
		if (bcc.getResultposter() != 1 && this.getResultposter() == 1)
			this.setResultposter(bcc.getResultposter());
		if (bcc.getHttpRetryTime() != 1 && this.getHttpRetryTime() == 1)
			this.setHttpRetryTime(bcc.getHttpRetryTime());
		if (bcc.getTaskPerTime() != 10 && this.getTaskPerTime() == 10)
			this.setTaskPerTime(bcc.getTaskPerTime());
		if (bcc.getResultPerTime() != 10 && this.getResultPerTime() == 10)
			this.setResultPerTime(bcc.getResultPerTime());
		if (bcc.isDetailedlog() || this.isDetailedlog())
			this.setDetailedlog(true);

		// 初始化相关变量结束
		try {
			common = CustomLogger.getLogger(appName + ".log");
			// String conf = "\r\n" + "appName:" + appName + ",taskgetter:" + taskgetter + ",worker:" + worker + ",resultposter:" + resultposter + ",detailedlog:" + detailedlog;

			if (appName.equals("justtest") || appName.equals("")) {
				common.warn("\r\n==================================================================\r\n" + bcc + "\r\n" + httpUtils + "\r\n" + pu + "\r\n" + this + "\r\n==================================================================\r\n");
				return;
			} else {
				common.warn("\r\n******************************************************************\r\n" + bcc + "\r\n" + httpUtils + "\r\n" + pu + "\r\n" + this + "\r\n******************************************************************\r\n");
			}
			monitor();
			ExecutorService es = Executors.newCachedThreadPool();
			for (int i = 0; i < taskgetter; i++) {
				es.execute(new TaskGetter());
			}
			for (int i = 0; i < worker; i++) {
				es.execute(new Worker());
			}
			for (int i = 0; i < resultposter; i++) {
				es.execute(new ResultPoster());
			}
			es.execute(new Monitor());
			es.shutdown();

		} catch (Exception e) {
			common.error(e.toString());
		}
	}

	protected abstract JSONObject check(JSONObject task);

	public JSONObject checkloop(JSONObject task) {
		JSONObject resultJson = null;
		for (int i = 0; i < httpRetryTime; i++) {
			resultJson = check(task);
			if (detailedlog)
				common.info(resultJson);
			if (!resultJson.getString("status").equals(Result.httpfail.name())) {
				break;
			}
		}
		return resultJson;
	}

	public class Worker implements Runnable {

		@Override
		public void run() {
			Random random = new Random();
			try {
				Thread.sleep(random.nextInt(30000));
				while (true) {
					JSONObject task = null;
					try {
						task = taskqueue.take();
					} catch (InterruptedException e) {
						throw new MyException(e);
					}
					JSONObject one = checkloop(task);
					resultqueue.put(one);
				}
			} catch (InterruptedException e) {
				throw new MyException(e);
			} catch (Exception e) {
				common.error(e);
			}
		}
	}

	public class TaskGetter implements Runnable {

		@Override
		public void run() {
			String urlString = taskurl + "?dbName=" + dbName + "&collName=" + collName + "&appName=" + appName + "&count=" + taskPerTime;
			if (resultColl != null)
				urlString += "&resultColl=" + resultColl;
			common.warn("taskurl:" + urlString);
			try {
				while (true) {
					JSONObject httpResult = httpUtils.httpget(urlString);
					String realResult = httpResult.getString("result");
					if (detailedlog)
						common.warn("jsonResult:" + appName + httpResult);
					if (httpResult.getIntValue("ec") == -1) {
						Thread.sleep(10000);
						continue;
					}
					JSONObject result = JSON.parseObject(realResult);
					if (result.getIntValue("ec") == 0) {
						if (result.getIntValue("count") > 0) {
							JSONArray jarray = result.getJSONArray("ones");
							for (int i = 0; i < jarray.size(); i++) {
								taskqueue.put(jarray.getJSONObject(i));
							}
						} else {
							// nomore task!
							Thread.sleep(20000);
						}
					} else {
						common.error(realResult);
					}
				}
			} catch (InterruptedException e) {
				throw new MyException(e);
			} catch (Exception e) {
				common.error(e);
			}
		}
	}

	public class ResultPoster implements Runnable {

		@Override
		public void run() {

			Map map = new HashMap();
			map.put("Accept", "application/json");
			map.put("Content-Type", "application/json");
			map.put("Cookie", "XSRF-TOKEN=09ac6089-2c10-4138-a18c-abfa7f6e99c1");
			map.put("X-XSRF-TOKEN", "09ac6089-2c10-4138-a18c-abfa7f6e99c1");
			try {
				while (true) {
					JSONObject json = new JSONObject();
					json.put("dbName", dbName);
					json.put("collName", collName);
					json.put("resultColl", resultColl);
					json.put("appName", appName);

					JSONArray result = new JSONArray();
					for (int i = 0; i < resultPerTime; i++) {
						JSONObject one = null;
						one = resultqueue.poll(1, TimeUnit.SECONDS);
						if (one != null) {
							result.add(one);
						}
					}
					if (result.size() == 0)
						continue;
					json.put("result", result);
					if (detailedlog)
						common.warn("ResultPost:" + json.toString());

					JSONObject httpResult = null;
					do {
						if (httpResult != null)
							Thread.sleep(1000);
						httpResult = httpUtils.httppost(resulturl, json.toString(), map);
						if (detailedlog)
							common.warn("ResultReturn:" + httpResult.toString());
					} while (httpResult.getIntValue("ec") == -1);
				}
			} catch (InterruptedException e) {
				throw new MyException(e);
			} catch (Exception e) {
				common.error(e);
			}
		}
	}

	public class Monitor implements Runnable {

		@Override
		public void run() {
			while (true) {
				if (taskqueue.size() != queuesize || resultqueue.size() != 0)
					common.warn(appName + " @@@@@@@current threads:" + Thread.activeCount() + "\ttaskqueue size:" + taskqueue.size() + "\tresultqueue size:" + resultqueue.size());
				try {
					Thread.sleep(10 * 1000);
				} catch (InterruptedException e) {
					throw new MyException(e);
				}
			}
		}
	}

	public String getAppName() {
		return appName;
	}

	public void setAppName(String appName) {
		this.appName = appName;
	}

	public int getTaskgetter() {
		return taskgetter;
	}

	public void setTaskgetter(int taskgetter) {
		this.taskgetter = taskgetter;
	}

	public int getWorker() {
		return worker;
	}

	public void setWorker(int worker) {
		this.worker = worker;
	}

	public int getResultposter() {
		return resultposter;
	}

	public void setResultposter(int resultposter) {
		this.resultposter = resultposter;
	}

	public boolean isDetailedlog() {
		return detailedlog;
	}

	public void setDetailedlog(boolean detailedlog) {
		this.detailedlog = detailedlog;
	}

	public HttpUtils getHttpUtils() {
		return httpUtils;
	}

	public void setHttpUtils(HttpUtils httpUtils) {
		this.httpUtils = httpUtils;
	}

	public int getTaskPerTime() {
		return taskPerTime;
	}

	public void setTaskPerTime(int taskPerTime) {
		this.taskPerTime = taskPerTime;
	}

	public int getResultPerTime() {
		return resultPerTime;
	}

	public void setResultPerTime(int resultPerTime) {
		this.resultPerTime = resultPerTime;
	}

	public int getHttpRetryTime() {
		return httpRetryTime;
	}

	public void setHttpRetryTime(int httpRetryTime) {
		this.httpRetryTime = httpRetryTime;
	}

	public String getDbName() {
		return dbName;
	}

	public void setDbName(String dbName) {
		this.dbName = dbName;
	}

	public String getCollName() {
		return collName;
	}

	public void setCollName(String collName) {
		this.collName = collName;
	}

	public String getResultColl() {
		return resultColl;
	}

	public void setResultColl(String resultColl) {
		this.resultColl = resultColl;
	}

	public BlockingQueue getTaskqueue() {
		return taskqueue;
	}

	public void setTaskqueue(BlockingQueue taskqueue) {
		this.taskqueue = taskqueue;
	}

	public BlockingQueue getResultqueue() {
		return resultqueue;
	}

	public void setResultqueue(BlockingQueue resultqueue) {
		this.resultqueue = resultqueue;
	}

	public int getConnecttime() {
		return connecttime;
	}

	public void setConnecttime(int connecttime) {
		this.connecttime = connecttime;
	}

	public int getReadtime() {
		return readtime;
	}

	public void setReadtime(int readtime) {
		this.readtime = readtime;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public ProxyUtils getPu() {
		return pu;
	}

	public void setPu(ProxyUtils pu) {
		this.pu = pu;
	}

	public String getMonitors() {
		return monitors;
	}

	public void setMonitors(String monitors) {
		this.monitors = monitors;
	}

	@Override
	public String toString() {
		return "AbstractChecker [common=" + common + ", queuesize=" + queuesize + ", taskqueue=" + taskqueue + ", resultqueue=" + resultqueue + ", httpUtils=" + httpUtils + ", bcc=" + bcc + ", pu=" + pu + ", taskurl=" + taskurl + ", resulturl=" + resulturl + ", showurl=" + showurl + ", appName=" + appName + ", dbName=" + dbName + ", collName=" + collName + ", resultColl=" + resultColl + ", monitors=" + monitors + ", taskgetter=" + taskgetter + ", worker=" + worker + ", resultposter=" + resultposter + ", detailedlog=" + detailedlog + ", httpRetryTime=" + httpRetryTime + ", taskPerTime=" + taskPerTime + ", resultPerTime=" + resultPerTime + ", connecttime=" + connecttime + ", readtime=" + readtime + ", type=" + type + "]";
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy