瀏覽代碼

HTTP远程调度交互重构

xueli.xue 9 年之前
父節點
當前提交
53fd312d37

+ 7 - 4
xxl-job-admin/src/main/java/com/xxl/job/controller/JobLogController.java 查看文件

16
 import org.springframework.web.bind.annotation.RequestParam;
16
 import org.springframework.web.bind.annotation.RequestParam;
17
 import org.springframework.web.bind.annotation.ResponseBody;
17
 import org.springframework.web.bind.annotation.ResponseBody;
18
 
18
 
19
+import com.xxl.job.client.util.HttpUtil.RemoteCallBack;
19
 import com.xxl.job.core.constant.Constants.JobGroupEnum;
20
 import com.xxl.job.core.constant.Constants.JobGroupEnum;
20
-import com.xxl.job.core.model.ReturnT;
21
 import com.xxl.job.core.model.XxlJobLog;
21
 import com.xxl.job.core.model.XxlJobLog;
22
 import com.xxl.job.dao.IXxlJobLogDao;
22
 import com.xxl.job.dao.IXxlJobLogDao;
23
 
23
 
73
 	
73
 	
74
 	@RequestMapping("/save")
74
 	@RequestMapping("/save")
75
 	@ResponseBody
75
 	@ResponseBody
76
-	public ReturnT<String> triggerLog(int trigger_log_id, String status, String msg) {
76
+	public RemoteCallBack triggerLog(int trigger_log_id, String status, String msg) {
77
+		RemoteCallBack callBack = new RemoteCallBack();
78
+		callBack.setStatus(RemoteCallBack.FAIL);
77
 		XxlJobLog log = xxlJobLogDao.load(trigger_log_id);
79
 		XxlJobLog log = xxlJobLogDao.load(trigger_log_id);
78
 		if (log!=null) {
80
 		if (log!=null) {
79
 			log.setHandleTime(new Date());
81
 			log.setHandleTime(new Date());
80
 			log.setHandleStatus(status);
82
 			log.setHandleStatus(status);
81
 			log.setHandleMsg(msg);
83
 			log.setHandleMsg(msg);
82
 			xxlJobLogDao.updateHandleInfo(log);
84
 			xxlJobLogDao.updateHandleInfo(log);
83
-			return ReturnT.SUCCESS;
85
+			callBack.setStatus(RemoteCallBack.SUCCESS);
86
+			return callBack;
84
 		}
87
 		}
85
-		return ReturnT.FAIL;
88
+		return callBack;
86
 	}
89
 	}
87
 	
90
 	
88
 }
91
 }

+ 4 - 4
xxl-job-admin/src/main/java/com/xxl/job/core/thread/JobMonitorHelper.java 查看文件

11
 import org.slf4j.Logger;
11
 import org.slf4j.Logger;
12
 import org.slf4j.LoggerFactory;
12
 import org.slf4j.LoggerFactory;
13
 
13
 
14
-import com.xxl.job.client.util.HttpUtil;
14
+import com.xxl.job.client.util.HttpUtil.RemoteCallBack;
15
 import com.xxl.job.core.model.XxlJobInfo;
15
 import com.xxl.job.core.model.XxlJobInfo;
16
 import com.xxl.job.core.model.XxlJobLog;
16
 import com.xxl.job.core.model.XxlJobLog;
17
 import com.xxl.job.core.util.DynamicSchedulerUtil;
17
 import com.xxl.job.core.util.DynamicSchedulerUtil;
40
 					if (jobLogId != null && jobLogId > 0) {
40
 					if (jobLogId != null && jobLogId > 0) {
41
 						XxlJobLog log = DynamicSchedulerUtil.xxlJobLogDao.load(jobLogId);
41
 						XxlJobLog log = DynamicSchedulerUtil.xxlJobLogDao.load(jobLogId);
42
 						if (log!=null) {
42
 						if (log!=null) {
43
-							if (HttpUtil.SUCCESS.equals(log.getTriggerStatus()) && StringUtils.isBlank(log.getHandleStatus())) {
43
+							if (RemoteCallBack.SUCCESS.equals(log.getTriggerStatus()) && StringUtils.isBlank(log.getHandleStatus())) {
44
 								JobMonitorHelper.monitor(jobLogId);
44
 								JobMonitorHelper.monitor(jobLogId);
45
 							}
45
 							}
46
-							if (HttpUtil.SUCCESS.equals(log.getTriggerStatus()) && HttpUtil.SUCCESS.equals(log.getHandleStatus())) {
46
+							if (RemoteCallBack.SUCCESS.equals(log.getTriggerStatus()) && RemoteCallBack.SUCCESS.equals(log.getHandleStatus())) {
47
 								// pass
47
 								// pass
48
 							}
48
 							}
49
-							if (HttpUtil.FAIL.equals(log.getTriggerStatus()) || HttpUtil.FAIL.equals(log.getHandleStatus())) {
49
+							if (RemoteCallBack.FAIL.equals(log.getTriggerStatus()) || RemoteCallBack.FAIL.equals(log.getHandleStatus())) {
50
 								String monotorKey = log.getJobGroup().concat("_").concat(log.getJobName());
50
 								String monotorKey = log.getJobGroup().concat("_").concat(log.getJobName());
51
 								Integer count = countMap.get(monotorKey);
51
 								Integer count = countMap.get(monotorKey);
52
 								if (count == null) {
52
 								if (count == null) {

+ 4 - 4
xxl-job-admin/src/main/java/com/xxl/job/service/job/LocalNomalJobBean.java 查看文件

15
 import org.springframework.scheduling.quartz.QuartzJobBean;
15
 import org.springframework.scheduling.quartz.QuartzJobBean;
16
 
16
 
17
 import com.xxl.job.client.handler.HandlerRepository;
17
 import com.xxl.job.client.handler.HandlerRepository;
18
-import com.xxl.job.client.util.HttpUtil;
18
+import com.xxl.job.client.util.HttpUtil.RemoteCallBack;
19
 import com.xxl.job.client.util.JacksonUtil;
19
 import com.xxl.job.client.util.JacksonUtil;
20
 import com.xxl.job.core.model.XxlJobInfo;
20
 import com.xxl.job.core.model.XxlJobInfo;
21
 import com.xxl.job.core.model.XxlJobLog;
21
 import com.xxl.job.core.model.XxlJobLog;
60
 		}
60
 		}
61
 		
61
 		
62
 		jobLog.setTriggerTime(new Date());
62
 		jobLog.setTriggerTime(new Date());
63
-		jobLog.setTriggerStatus(HttpUtil.SUCCESS);
63
+		jobLog.setTriggerStatus(RemoteCallBack.SUCCESS);
64
 		jobLog.setTriggerMsg(null);
64
 		jobLog.setTriggerMsg(null);
65
 		
65
 		
66
 		try {
66
 		try {
67
 			Object responseMsg = this.handle(handlerParams);
67
 			Object responseMsg = this.handle(handlerParams);
68
 			
68
 			
69
 			jobLog.setHandleTime(new Date());
69
 			jobLog.setHandleTime(new Date());
70
-			jobLog.setHandleStatus(HttpUtil.SUCCESS);
70
+			jobLog.setHandleStatus(RemoteCallBack.SUCCESS);
71
 			jobLog.setHandleMsg(JacksonUtil.writeValueAsString(responseMsg));
71
 			jobLog.setHandleMsg(JacksonUtil.writeValueAsString(responseMsg));
72
 		} catch (Exception e) {
72
 		} catch (Exception e) {
73
 			logger.info("HandlerThread Exception:", e);
73
 			logger.info("HandlerThread Exception:", e);
75
 			e.printStackTrace(new PrintWriter(out));
75
 			e.printStackTrace(new PrintWriter(out));
76
 			
76
 			
77
 			jobLog.setHandleTime(new Date());
77
 			jobLog.setHandleTime(new Date());
78
-			jobLog.setHandleStatus(HttpUtil.FAIL);
78
+			jobLog.setHandleStatus(RemoteCallBack.FAIL);
79
 			jobLog.setHandleMsg(out.toString());
79
 			jobLog.setHandleMsg(out.toString());
80
 		}
80
 		}
81
 		
81
 		

+ 10 - 20
xxl-job-admin/src/main/java/com/xxl/job/service/job/RemoteHttpJobBean.java 查看文件

4
 import java.util.HashMap;
4
 import java.util.HashMap;
5
 import java.util.Map;
5
 import java.util.Map;
6
 
6
 
7
-import org.apache.commons.lang.StringUtils;
8
 import org.quartz.DisallowConcurrentExecution;
7
 import org.quartz.DisallowConcurrentExecution;
9
 import org.quartz.JobExecutionContext;
8
 import org.quartz.JobExecutionContext;
10
 import org.quartz.JobExecutionException;
9
 import org.quartz.JobExecutionException;
15
 
14
 
16
 import com.xxl.job.client.handler.HandlerRepository;
15
 import com.xxl.job.client.handler.HandlerRepository;
17
 import com.xxl.job.client.util.HttpUtil;
16
 import com.xxl.job.client.util.HttpUtil;
17
+import com.xxl.job.client.util.HttpUtil.RemoteCallBack;
18
 import com.xxl.job.client.util.JacksonUtil;
18
 import com.xxl.job.client.util.JacksonUtil;
19
 import com.xxl.job.core.model.XxlJobInfo;
19
 import com.xxl.job.core.model.XxlJobInfo;
20
 import com.xxl.job.core.model.XxlJobLog;
20
 import com.xxl.job.core.model.XxlJobLog;
60
 		params.put(HandlerRepository.HANDLER_NAME, jobDataMap.get(HandlerRepository.HANDLER_NAME));
60
 		params.put(HandlerRepository.HANDLER_NAME, jobDataMap.get(HandlerRepository.HANDLER_NAME));
61
 		params.put(HandlerRepository.HANDLER_PARAMS, jobDataMap.get(HandlerRepository.HANDLER_PARAMS));
61
 		params.put(HandlerRepository.HANDLER_PARAMS, jobDataMap.get(HandlerRepository.HANDLER_PARAMS));
62
 
62
 
63
-		// handler address, netty or servlet
63
+		// handler address, jetty or servlet
64
 		String handler_address = jobDataMap.get(HandlerRepository.HANDLER_ADDRESS);
64
 		String handler_address = jobDataMap.get(HandlerRepository.HANDLER_ADDRESS);
65
 		if (!handler_address.startsWith("http")){
65
 		if (!handler_address.startsWith("http")){
66
 			handler_address = "http://" + handler_address + "/";
66
 			handler_address = "http://" + handler_address + "/";
67
 		}
67
 		}
68
 
68
 
69
-		String[] postResp = HttpUtil.post(handler_address, params);
70
-		logger.info(">>>>>>>>>>> xxl-job trigger http response, jobLog.id:{}, jobLog:{}", jobLog.getId(), jobLog);
69
+		RemoteCallBack callback = HttpUtil.post(handler_address, params);
70
+		logger.info(">>>>>>>>>>> xxl-job trigger http response, jobLog.id:{}, jobLog:{}, callback:{}", jobLog.getId(), jobLog, callback);
71
 
71
 
72
-		// parse trigger response
73
-		String responseMsg = postResp[0];
74
-		String exceptionMsg = postResp[1];
75
-		
76
-		jobLog.setTriggerTime(new Date());
77
-		jobLog.setTriggerStatus(HttpUtil.FAIL);
78
-		jobLog.setTriggerMsg("[responseMsg]:"+responseMsg+"<br>[exceptionMsg]:"+exceptionMsg);
79
-		if (StringUtils.isNotBlank(responseMsg) && responseMsg.indexOf("{")>-1 ) {
80
-			Map<String, String> responseMap = JacksonUtil.readValue(responseMsg, Map.class);
81
-			if (responseMap!=null && StringUtils.isNotBlank(responseMap.get(HttpUtil.status))) {
82
-				jobLog.setTriggerStatus(responseMap.get(HttpUtil.status));
83
-				jobLog.setTriggerMsg(responseMap.get(HttpUtil.msg));
84
-			}
85
-		}
86
-		
87
 		// update trigger info
72
 		// update trigger info
73
+		jobLog.setTriggerTime(new Date());
74
+		jobLog.setTriggerStatus(callback.getStatus());
75
+		jobLog.setTriggerMsg(callback.getMsg());
88
 		DynamicSchedulerUtil.xxlJobLogDao.updateTriggerInfo(jobLog);
76
 		DynamicSchedulerUtil.xxlJobLogDao.updateTriggerInfo(jobLog);
77
+
78
+		// monitor triger
89
 		JobMonitorHelper.monitor(jobLog.getId());
79
 		JobMonitorHelper.monitor(jobLog.getId());
90
-		logger.info(">>>>>>>>>>> xxl-job trigger end, jobLog.id:{}, jobLog:{}", jobLog.getId(), jobLog);
91
 		
80
 		
81
+		logger.info(">>>>>>>>>>> xxl-job trigger end, jobLog.id:{}, jobLog:{}", jobLog.getId(), jobLog);
92
     }
82
     }
93
 	
83
 	
94
 }
84
 }

+ 2 - 2
xxl-job-admin/src/test/java/com/xxl/job/dao/impl/XxlJobLogTest.java 查看文件

11
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
11
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
12
 
12
 
13
 import com.xxl.job.client.handler.IJobHandler;
13
 import com.xxl.job.client.handler.IJobHandler;
14
-import com.xxl.job.client.util.HttpUtil;
14
+import com.xxl.job.client.util.HttpUtil.RemoteCallBack;
15
 import com.xxl.job.core.model.XxlJobLog;
15
 import com.xxl.job.core.model.XxlJobLog;
16
 import com.xxl.job.dao.IXxlJobLogDao;
16
 import com.xxl.job.dao.IXxlJobLogDao;
17
 
17
 
41
 	public void updateTriggerInfo(){
41
 	public void updateTriggerInfo(){
42
 		XxlJobLog xxlJobLog = xxlJobLogDao.load(29);
42
 		XxlJobLog xxlJobLog = xxlJobLogDao.load(29);
43
 		xxlJobLog.setTriggerTime(new Date());
43
 		xxlJobLog.setTriggerTime(new Date());
44
-		xxlJobLog.setTriggerStatus(HttpUtil.SUCCESS);
44
+		xxlJobLog.setTriggerStatus(RemoteCallBack.SUCCESS);
45
 		xxlJobLog.setTriggerMsg("trigger msg");
45
 		xxlJobLog.setTriggerMsg("trigger msg");
46
 		xxlJobLogDao.updateTriggerInfo(xxlJobLog);
46
 		xxlJobLogDao.updateTriggerInfo(xxlJobLog);
47
 	}
47
 	}

+ 9 - 16
xxl-job-client/src/main/java/com/xxl/job/client/handler/HandlerRepository.java 查看文件

1
 package com.xxl.job.client.handler;
1
 package com.xxl.job.client.handler;
2
 
2
 
3
-import java.util.HashMap;
4
 import java.util.Map;
3
 import java.util.Map;
5
 import java.util.concurrent.ConcurrentHashMap;
4
 import java.util.concurrent.ConcurrentHashMap;
6
 
5
 
7
 import org.slf4j.Logger;
6
 import org.slf4j.Logger;
8
 import org.slf4j.LoggerFactory;
7
 import org.slf4j.LoggerFactory;
9
 
8
 
10
-import com.xxl.job.client.util.HttpUtil;
9
+import com.xxl.job.client.util.HttpUtil.RemoteCallBack;
11
 import com.xxl.job.client.util.JacksonUtil;
10
 import com.xxl.job.client.util.JacksonUtil;
12
 
11
 
13
 /**
12
 /**
38
 	public static String pushHandleQueue(Map<String, String> _param) {
37
 	public static String pushHandleQueue(Map<String, String> _param) {
39
 		logger.info(">>>>>>>>>>> xxl-job pushHandleQueue start, _param:{}", new Object[]{_param});
38
 		logger.info(">>>>>>>>>>> xxl-job pushHandleQueue start, _param:{}", new Object[]{_param});
40
 		
39
 		
41
-		// result
42
-		String _status = HttpUtil.FAIL;
43
-		String _msg = "";
40
+		// callback
41
+		RemoteCallBack callback = new RemoteCallBack();
42
+		callback.setStatus(RemoteCallBack.FAIL);
44
 		
43
 		
45
 		// push data to queue
44
 		// push data to queue
46
 		String handler_name = _param.get(HandlerRepository.HANDLER_NAME);
45
 		String handler_name = _param.get(HandlerRepository.HANDLER_NAME);
48
 			HandlerThread handlerThread = handlerTreadMap.get(handler_name);
47
 			HandlerThread handlerThread = handlerTreadMap.get(handler_name);
49
 			if (handlerThread != null) {
48
 			if (handlerThread != null) {
50
 				handlerThread.pushData(_param);
49
 				handlerThread.pushData(_param);
51
-				_status = HttpUtil.SUCCESS;
50
+				callback.setStatus(RemoteCallBack.SUCCESS);
52
 			} else {
51
 			} else {
53
-				_msg = "handler not found.";
52
+				callback.setMsg("handler[" + handler_name + "] not found.");
54
 			}
53
 			}
55
 		}else{
54
 		}else{
56
-			_msg = "param[HANDLER_NAME] not exists.";
55
+			callback.setMsg("param[HANDLER_NAME] can not be null.");
57
 		}
56
 		}
58
 		
57
 		
59
-		
60
-		HashMap<String, String> triggerData = new HashMap<String, String>();
61
-		triggerData.put(HandlerRepository.TRIGGER_LOG_ID, _param.get(HandlerRepository.TRIGGER_LOG_ID));
62
-		triggerData.put(HttpUtil.status, _status);
63
-		triggerData.put(HttpUtil.msg, _msg);
64
-		
65
-		logger.info(">>>>>>>>>>> xxl-job pushHandleQueue end, triggerData:{}", new Object[]{triggerData});
66
-		return JacksonUtil.writeValueAsString(triggerData);
58
+		logger.info(">>>>>>>>>>> xxl-job pushHandleQueue end, triggerData:{}", new Object[]{callback});
59
+		return JacksonUtil.writeValueAsString(callback);
67
 	}
60
 	}
68
 	
61
 	
69
 }
62
 }

+ 7 - 6
xxl-job-client/src/main/java/com/xxl/job/client/handler/HandlerThread.java 查看文件

12
 
12
 
13
 import com.xxl.job.client.handler.IJobHandler.JobHandleStatus;
13
 import com.xxl.job.client.handler.IJobHandler.JobHandleStatus;
14
 import com.xxl.job.client.util.HttpUtil;
14
 import com.xxl.job.client.util.HttpUtil;
15
+import com.xxl.job.client.util.HttpUtil.RemoteCallBack;
15
 
16
 
16
 /**
17
 /**
17
  * handler thread
18
  * handler thread
65
 					}
66
 					}
66
 
67
 
67
 					// callback handler info
68
 					// callback handler info
68
-					String callback_response[] = null;
69
+					RemoteCallBack callback = null;
69
 					try {
70
 					try {
70
 						
71
 						
71
 						HashMap<String, String> params = new HashMap<String, String>();
72
 						HashMap<String, String> params = new HashMap<String, String>();
72
 						params.put(HandlerRepository.TRIGGER_LOG_ID, trigger_log_id);
73
 						params.put(HandlerRepository.TRIGGER_LOG_ID, trigger_log_id);
73
-						params.put(HttpUtil.status, _status.name());
74
-						params.put(HttpUtil.msg, _msg);
75
-						callback_response = HttpUtil.post(trigger_log_url, params);
74
+						params.put("status", _status.name());
75
+						params.put("msg", _msg);
76
+						callback = HttpUtil.post(trigger_log_url, params);
76
 					} catch (Exception e) {
77
 					} catch (Exception e) {
77
 						logger.info("HandlerThread Exception:", e);
78
 						logger.info("HandlerThread Exception:", e);
78
 					}
79
 					}
79
-					logger.info("<<<<<<<<<<< xxl-job thread handle, handlerData:{}, callback_status:{}, callback_msg:{}, callback_response:{}, thread:{}", 
80
-							new Object[]{handlerData, _status, _msg, callback_response, this});
80
+					logger.info("<<<<<<<<<<< xxl-job thread handle, handlerData:{}, callback_status:{}, callback_msg:{}, callback:{}, thread:{}", 
81
+							new Object[]{handlerData, _status, _msg, callback, this});
81
 				} else {
82
 				} else {
82
 					try {
83
 					try {
83
 						TimeUnit.MILLISECONDS.sleep(i * 100);
84
 						TimeUnit.MILLISECONDS.sleep(i * 100);

+ 40 - 20
xxl-job-client/src/main/java/com/xxl/job/client/util/HttpUtil.java 查看文件

25
  */
25
  */
26
 public class HttpUtil {
26
 public class HttpUtil {
27
 	
27
 	
28
-	// response param
29
-	public static final String status = "status";
30
-	public static final String msg = "msg";
31
-	// response status enum
32
-	public static final String SUCCESS = "SUCCESS";
33
-	public static final String FAIL = "FAIL";
28
+	/**
29
+	 * http remote callback
30
+	 */
31
+	public static class RemoteCallBack{
32
+		public static final String SUCCESS = "SUCCESS";
33
+		public static final String FAIL = "FAIL";
34
+		
35
+		private String status;
36
+		private String msg;
37
+		public void setStatus(String status) {
38
+			this.status = status;
39
+		}
40
+		public String getStatus() {
41
+			return status;
42
+		}
43
+		public void setMsg(String msg) {
44
+			this.msg = msg;
45
+		}
46
+		public String getMsg() {
47
+			return msg;
48
+		}
49
+	}
34
 	
50
 	
35
 	/**
51
 	/**
36
 	 * http post request
52
 	 * http post request
38
 	 * @param params
54
 	 * @param params
39
 	 * @return	[0]=responseMsg, [1]=exceptionMsg
55
 	 * @return	[0]=responseMsg, [1]=exceptionMsg
40
 	 */
56
 	 */
41
-	public static String[] post(String reqURL, Map<String, String> params){
42
-		String responseMsg = null;
43
-		String exceptionMsg = null;
57
+	public static RemoteCallBack post(String reqURL, Map<String, String> params){
58
+		RemoteCallBack callback = new RemoteCallBack();
59
+		callback.setStatus(RemoteCallBack.FAIL);
44
 		
60
 		
45
 		// do post
61
 		// do post
46
 		HttpPost httpPost = null;
62
 		HttpPost httpPost = null;
60
 			
76
 			
61
 			HttpResponse response = httpClient.execute(httpPost);
77
 			HttpResponse response = httpClient.execute(httpPost);
62
 			HttpEntity entity = response.getEntity();
78
 			HttpEntity entity = response.getEntity();
63
-			if (null != entity) {
64
-				responseMsg = EntityUtils.toString(entity, "UTF-8");
65
-				EntityUtils.consume(entity);
66
-			}
67
-			if (response.getStatusLine().getStatusCode() != 200) {
68
-				exceptionMsg = "response.getStatusLine().getStatusCode() = " + response.getStatusLine().getStatusCode();
79
+			if (response.getStatusLine().getStatusCode() == 200) {
80
+				if (null != entity) {
81
+					String responseMsg = EntityUtils.toString(entity, "UTF-8");
82
+					callback = JacksonUtil.readValue(responseMsg, RemoteCallBack.class);
83
+					if (callback == null) {
84
+						callback = new RemoteCallBack();
85
+						callback.setStatus(RemoteCallBack.FAIL);
86
+						callback.setMsg("responseMsg parse json fail, responseMsg:" + responseMsg);
87
+					}
88
+					EntityUtils.consume(entity);
89
+				}
90
+			} else {
91
+				callback.setMsg("http statusCode error, statusCode:" + response.getStatusLine().getStatusCode());
69
 			}
92
 			}
70
 		} catch (Exception e) {
93
 		} catch (Exception e) {
71
 			e.printStackTrace();
94
 			e.printStackTrace();
72
 			StringWriter out = new StringWriter();
95
 			StringWriter out = new StringWriter();
73
 			e.printStackTrace(new PrintWriter(out));
96
 			e.printStackTrace(new PrintWriter(out));
74
-			exceptionMsg = out.toString();
97
+			callback.setMsg(out.toString());
75
 		} finally{
98
 		} finally{
76
 			if (httpPost!=null) {
99
 			if (httpPost!=null) {
77
 				httpPost.releaseConnection();
100
 				httpPost.releaseConnection();
85
 			}
108
 			}
86
 		}
109
 		}
87
 		
110
 		
88
-		String[] result = new String[2];
89
-		result[0] = responseMsg;
90
-		result[1] = exceptionMsg;
91
-		return result;
111
+		return callback;
92
 	}
112
 	}
93
 }
113
 }