瀏覽代碼

JobHandler支持自定义回调结果;

xueli.xue 8 年之前
父節點
當前提交
b7b411ce2a

+ 3 - 1
README.md 查看文件

772
 
772
 
773
 #### 6.12 版本 V1.6.2 特性(Coding)
773
 #### 6.12 版本 V1.6.2 特性(Coding)
774
 - 1、任务报表:总任务数、总调度数、调度成功比例;
774
 - 1、任务报表:总任务数、总调度数、调度成功比例;
775
+- 2、JobHandler支持自定义回调结果;
775
 
776
 
776
 #### TODO LIST
777
 #### TODO LIST
777
 - 1、支持脚本JOB(源码或指定路径), 即shell/python/php等, 日志实时输出并支持在线监控;定制JobHandler实现;
778
 - 1、支持脚本JOB(源码或指定路径), 即shell/python/php等, 日志实时输出并支持在线监控;定制JobHandler实现;
779
 - 3、任务权限管理;
780
 - 3、任务权限管理;
780
 - 4、执行器,server启动,注册逻辑调整;
781
 - 4、执行器,server启动,注册逻辑调整;
781
 - 5、调度失败重试机制;
782
 - 5、调度失败重试机制;
782
-- 6、JobHandler支持自定义回调结果;
783
+
784
+- 7、JobHandler开启多线程时,支持记录执行日志;
783
 
785
 
784
 
786
 
785
 ## 七、其他
787
 ## 七、其他

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

120
 			ReturnT<LogResult> logResult = executorBiz.log(triggerTime, logId, fromLineNum);
120
 			ReturnT<LogResult> logResult = executorBiz.log(triggerTime, logId, fromLineNum);
121
 
121
 
122
 			// is end
122
 			// is end
123
-			if (logResult.getContent()!=null && logResult.getContent().getFromLineNum() > logResult.getContent().getToLineNum()) {
124
-				XxlJobLog jobLog = xxlJobLogDao.load(logId);
125
-				if (jobLog.getHandleCode() > 0) {
126
-					logResult.getContent().setEnd(true);
127
-				}
128
-			}
123
+			/*XxlJobLog jobLog = xxlJobLogDao.load(logId);
124
+			if (jobLog.getHandleCode() > 0) {
125
+				logResult.getContent().setEnd(true);
126
+			}*/
129
 
127
 
130
 			return logResult;
128
 			return logResult;
131
 		} catch (Exception e) {
129
 		} catch (Exception e) {

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

31
 
31
 
32
         // trigger success, to trigger child job, and avoid repeat trigger child job
32
         // trigger success, to trigger child job, and avoid repeat trigger child job
33
         String childTriggerMsg = null;
33
         String childTriggerMsg = null;
34
-        if (ReturnT.SUCCESS_CODE==handleCallbackParam.getCode() && ReturnT.SUCCESS_CODE!=log.getHandleCode()) {
34
+        if (ReturnT.SUCCESS_CODE==handleCallbackParam.getExecuteResult().getCode() && ReturnT.SUCCESS_CODE!=log.getHandleCode()) {
35
             XxlJobInfo xxlJobInfo = XxlJobDynamicScheduler.xxlJobInfoDao.loadById(log.getJobId());
35
             XxlJobInfo xxlJobInfo = XxlJobDynamicScheduler.xxlJobInfoDao.loadById(log.getJobId());
36
             if (xxlJobInfo!=null && StringUtils.isNotBlank(xxlJobInfo.getChildJobKey())) {
36
             if (xxlJobInfo!=null && StringUtils.isNotBlank(xxlJobInfo.getChildJobKey())) {
37
                 childTriggerMsg = "<hr>";
37
                 childTriggerMsg = "<hr>";
68
         if (log.getHandleMsg()!=null) {
68
         if (log.getHandleMsg()!=null) {
69
             handleMsg.append(log.getHandleMsg()).append("<br>");
69
             handleMsg.append(log.getHandleMsg()).append("<br>");
70
         }
70
         }
71
-        if (handleCallbackParam.getMsg() != null) {
72
-            handleMsg.append("执行备注:").append(handleCallbackParam.getMsg());
71
+        if (handleCallbackParam.getExecuteResult().getMsg() != null) {
72
+            handleMsg.append("执行备注:").append(handleCallbackParam.getExecuteResult().getMsg());
73
         }
73
         }
74
         if (childTriggerMsg !=null) {
74
         if (childTriggerMsg !=null) {
75
             handleMsg.append("<br>子任务触发备注:").append(childTriggerMsg);
75
             handleMsg.append("<br>子任务触发备注:").append(childTriggerMsg);
77
 
77
 
78
         // success, save log
78
         // success, save log
79
         log.setHandleTime(new Date());
79
         log.setHandleTime(new Date());
80
-        log.setHandleCode(handleCallbackParam.getCode());
80
+        log.setHandleCode(handleCallbackParam.getExecuteResult().getCode());
81
         log.setHandleMsg(handleMsg.toString());
81
         log.setHandleMsg(handleMsg.toString());
82
         XxlJobDynamicScheduler.xxlJobLogDao.updateHandleInfo(log);
82
         XxlJobDynamicScheduler.xxlJobLogDao.updateHandleInfo(log);
83
 
83
 

+ 3 - 1
xxl-job-admin/src/main/webapp/WEB-INF/template/jobinfo/jobinfo.index.ftl 查看文件

172
 import org.slf4j.Logger;
172
 import org.slf4j.Logger;
173
 import org.slf4j.LoggerFactory;
173
 import org.slf4j.LoggerFactory;
174
 
174
 
175
+import com.xxl.job.core.biz.model.ReturnT;
175
 import com.xxl.job.core.handler.IJobHandler;
176
 import com.xxl.job.core.handler.IJobHandler;
176
 
177
 
177
 public class DemoGlueJobHandler extends IJobHandler {
178
 public class DemoGlueJobHandler extends IJobHandler {
178
 	private static transient Logger logger = LoggerFactory.getLogger(DemoGlueJobHandler.class);
179
 	private static transient Logger logger = LoggerFactory.getLogger(DemoGlueJobHandler.class);
179
 
180
 
180
 	@Override
181
 	@Override
181
-	public void execute(String... params) throws Exception {
182
+	public ReturnT<String> execute(String... params) throws Exception {
182
 		logger.info("XXL-JOB, Hello World.");
183
 		logger.info("XXL-JOB, Hello World.");
184
+		return ReturnT.SUCCESS;
183
 	}
185
 	}
184
 
186
 
185
 }
187
 }

+ 5 - 3
xxl-job-admin/src/main/webapp/static/js/joblog.detail.1.js 查看文件

8
     }
8
     }
9
 
9
 
10
     // pull log
10
     // pull log
11
-    var fromLineNum = 0;    // [from, to]
11
+    var fromLineNum = 1;    // [from, to], start as 1
12
     var pullFailCount = 0;
12
     var pullFailCount = 0;
13
     function pullLog() {
13
     function pullLog() {
14
         // pullFailCount, max=20
14
         // pullFailCount, max=20
44
                     }
44
                     }
45
                     if (fromLineNum > data.content.toLineNum ) {
45
                     if (fromLineNum > data.content.toLineNum ) {
46
                         console.log('pullLog already line-end');
46
                         console.log('pullLog already line-end');
47
+
47
                         // valid end
48
                         // valid end
48
                         if (data.content.end) {
49
                         if (data.content.end) {
49
                             logRunStop('<span style="color: green;">[Rolling Log Finish]</span>');
50
                             logRunStop('<span style="color: green;">[Rolling Log Finish]</span>');
50
                             return;
51
                             return;
51
                         }
52
                         }
53
+
52
                         return;
54
                         return;
53
                     }
55
                     }
54
 
56
 
55
-                    // append
57
+                    // append content
56
                     fromLineNum = data.content.toLineNum + 1;
58
                     fromLineNum = data.content.toLineNum + 1;
57
                     $('#logConsole').append(data.content.logContent);
59
                     $('#logConsole').append(data.content.logContent);
58
                     pullFailCount = 0;
60
                     pullFailCount = 0;
72
 
74
 
73
     // handler already callback, end
75
     // handler already callback, end
74
     if (handleCode > 0) {
76
     if (handleCode > 0) {
75
-        logRunStop('<span style="color: green;">[Load Log Finish]</span>');
77
+        logRunStop('<br><span style="color: green;">[Load Log Finish]</span>');
76
         return;
78
         return;
77
     }
79
     }
78
 
80
 

+ 7 - 18
xxl-job-core/src/main/java/com/xxl/job/core/biz/model/HandleCallbackParam.java 查看文件

12
     private int logId;
12
     private int logId;
13
     private Set<String> logAddress;
13
     private Set<String> logAddress;
14
 
14
 
15
-    private int code;
16
-    private String msg;
15
+    private ReturnT<String> executeResult;
17
 
16
 
18
-    public HandleCallbackParam(int logId, Set<String> logAddress, int code, String msg) {
17
+    public HandleCallbackParam(int logId, Set<String> logAddress, ReturnT<String> executeResult) {
19
         this.logId = logId;
18
         this.logId = logId;
20
         this.logAddress = logAddress;
19
         this.logAddress = logAddress;
21
-        this.code = code;
22
-        this.msg = msg;
20
+        this.executeResult = executeResult;
23
     }
21
     }
24
 
22
 
25
     public int getLogId() {
23
     public int getLogId() {
38
         this.logAddress = logAddress;
36
         this.logAddress = logAddress;
39
     }
37
     }
40
 
38
 
41
-    public int getCode() {
42
-        return code;
39
+    public ReturnT<String> getExecuteResult() {
40
+        return executeResult;
43
     }
41
     }
44
 
42
 
45
-    public void setCode(int code) {
46
-        this.code = code;
43
+    public void setExecuteResult(ReturnT<String> executeResult) {
44
+        this.executeResult = executeResult;
47
     }
45
     }
48
-
49
-    public String getMsg() {
50
-        return msg;
51
-    }
52
-
53
-    public void setMsg(String msg) {
54
-        this.msg = msg;
55
-    }
56
-
57
 }
46
 }

+ 6 - 4
xxl-job-core/src/main/java/com/xxl/job/core/handler/IJobHandler.java 查看文件

1
 package com.xxl.job.core.handler;
1
 package com.xxl.job.core.handler;
2
 
2
 
3
+import com.xxl.job.core.biz.model.ReturnT;
4
+
3
 /**
5
 /**
4
  * remote job handler
6
  * remote job handler
5
  * @author xuxueli 2015-12-19 19:06:38
7
  * @author xuxueli 2015-12-19 19:06:38
7
 public abstract class IJobHandler {
9
 public abstract class IJobHandler {
8
 	
10
 	
9
 	/**
11
 	/**
10
-	 * job handler <br><br>
11
-	 * the return Object will be and stored
12
+	 * job handler
12
 	 * @param params
13
 	 * @param params
13
-	 * @throws Exception  default sussecc, fail if catch exception
14
+	 * @return
15
+	 * @throws Exception
14
 	 */
16
 	 */
15
-	public abstract void execute(String... params) throws Exception;
17
+	public abstract ReturnT<String> execute(String... params) throws Exception;
16
 	
18
 	
17
 }
19
 }

+ 3 - 1
xxl-job-core/src/main/java/com/xxl/job/core/handler/impl/GlueJobHandler.java 查看文件

1
 package com.xxl.job.core.handler.impl;
1
 package com.xxl.job.core.handler.impl;
2
 
2
 
3
+import com.xxl.job.core.biz.model.ReturnT;
3
 import com.xxl.job.core.handler.IJobHandler;
4
 import com.xxl.job.core.handler.IJobHandler;
4
 import org.slf4j.Logger;
5
 import org.slf4j.Logger;
5
 import org.slf4j.LoggerFactory;
6
 import org.slf4j.LoggerFactory;
22
 	}
23
 	}
23
 
24
 
24
 	@Override
25
 	@Override
25
-	public void execute(String... params) throws Exception {
26
+	public ReturnT<String> execute(String... params) throws Exception {
26
 		logger.info("----------- glue.version:{} -----------", glueUpdatetime);
27
 		logger.info("----------- glue.version:{} -----------", glueUpdatetime);
27
 		jobHandler.execute(params);
28
 		jobHandler.execute(params);
29
+		return ReturnT.SUCCESS;
28
 	}
30
 	}
29
 
31
 
30
 }
32
 }

+ 4 - 4
xxl-job-core/src/main/java/com/xxl/job/core/log/XxlJobFileAppender.java 查看文件

122
 
122
 
123
 		// valid log file
123
 		// valid log file
124
 		if (logFileName==null || logFileName.trim().length()==0) {
124
 		if (logFileName==null || logFileName.trim().length()==0) {
125
-            return new LogResult(fromLineNum, -1, "readLog fail, logFile not found", true);
125
+            return new LogResult(fromLineNum, 0, "readLog fail, logFile not found", true);
126
 		}
126
 		}
127
 		File logFile = new File(filePath, logFileName);
127
 		File logFile = new File(filePath, logFileName);
128
 
128
 
129
 		if (!logFile.exists()) {
129
 		if (!logFile.exists()) {
130
-            return new LogResult(fromLineNum, -1, "readLog fail, logFile not exists", true);
130
+            return new LogResult(fromLineNum, 0, "readLog fail, logFile not exists", true);
131
 		}
131
 		}
132
 
132
 
133
 		// read file
133
 		// read file
139
 			String line = null;
139
 			String line = null;
140
 
140
 
141
 			while ((line = reader.readLine())!=null) {
141
 			while ((line = reader.readLine())!=null) {
142
-				toLineNum++;
143
-				if (reader.getLineNumber() >= fromLineNum) {
142
+				toLineNum = reader.getLineNumber();		// [from, to], start as 1
143
+				if (toLineNum >= fromLineNum) {
144
 					logContentBuffer.append(line).append("\n");
144
 					logContentBuffer.append(line).append("\n");
145
 				}
145
 				}
146
 			}
146
 			}

+ 14 - 10
xxl-job-core/src/main/java/com/xxl/job/core/thread/JobThread.java 查看文件

74
 							? (String[])(Arrays.asList(triggerParam.getExecutorParams().split(",")).toArray()) : null;
74
 							? (String[])(Arrays.asList(triggerParam.getExecutorParams().split(",")).toArray()) : null;
75
 					
75
 					
76
 					// handle job
76
 					// handle job
77
-					int _code = ReturnT.SUCCESS_CODE;
78
-					String _msg = null;
79
-
77
+					ReturnT<String> executeResult = null;
80
 					try {
78
 					try {
81
 						// log filename: yyyy-MM-dd/9999.log
79
 						// log filename: yyyy-MM-dd/9999.log
82
 						String logFileName = XxlJobFileAppender.makeLogFileName(new Date(triggerParam.getLogDateTim()), triggerParam.getLogId());
80
 						String logFileName = XxlJobFileAppender.makeLogFileName(new Date(triggerParam.getLogDateTim()), triggerParam.getLogId());
83
 
81
 
84
 						XxlJobFileAppender.contextHolder.set(logFileName);
82
 						XxlJobFileAppender.contextHolder.set(logFileName);
85
 						logger.info("----------- xxl-job job execute start -----------");
83
 						logger.info("----------- xxl-job job execute start -----------");
86
-						handler.execute(handlerParams);
84
+
85
+						executeResult = handler.execute(handlerParams);
86
+						if (executeResult == null) {
87
+							executeResult = ReturnT.FAIL;
88
+						}
87
 					} catch (Exception e) {
89
 					} catch (Exception e) {
88
 						logger.error("JobThread Exception:", e);
90
 						logger.error("JobThread Exception:", e);
89
-						_code = ReturnT.FAIL_CODE;
90
 						StringWriter out = new StringWriter();
91
 						StringWriter out = new StringWriter();
91
 						e.printStackTrace(new PrintWriter(out));
92
 						e.printStackTrace(new PrintWriter(out));
92
-						_msg = out.toString();
93
+
94
+						executeResult = new ReturnT<String>(ReturnT.FAIL_CODE, out.toString());
93
 					}
95
 					}
94
 					logger.info("----------- xxl-job job execute end ----------- <br> Look : ExecutorParams:{}, Code:{}, Msg:{}",
96
 					logger.info("----------- xxl-job job execute end ----------- <br> Look : ExecutorParams:{}, Code:{}, Msg:{}",
95
-							new Object[]{handlerParams, _code, _msg});
97
+							new Object[]{handlerParams, executeResult.getCode(), executeResult.getMsg()});
96
 					
98
 					
97
 					// callback handler info
99
 					// callback handler info
98
 					if (!toStop) {
100
 					if (!toStop) {
99
 						// commonm
101
 						// commonm
100
-						TriggerCallbackThread.pushCallBack(new HandleCallbackParam(triggerParam.getLogId(), triggerParam.getLogAddress(), _code, _msg));
102
+						TriggerCallbackThread.pushCallBack(new HandleCallbackParam(triggerParam.getLogId(), triggerParam.getLogAddress(), executeResult));
101
 					} else {
103
 					} else {
102
 						// is killed
104
 						// is killed
103
-						TriggerCallbackThread.pushCallBack(new HandleCallbackParam(triggerParam.getLogId(), triggerParam.getLogAddress(), ReturnT.FAIL_CODE, stopReason + " [业务运行中,被强制终止]"));
105
+						ReturnT stopResult = new ReturnT<String>(ReturnT.FAIL_CODE, stopReason + " [业务运行中,被强制终止]");
106
+						TriggerCallbackThread.pushCallBack(new HandleCallbackParam(triggerParam.getLogId(), triggerParam.getLogAddress(), stopResult));
104
 					}
107
 					}
105
 				}
108
 				}
106
 			} catch (Exception e) {
109
 			} catch (Exception e) {
113
 			TriggerParam triggerParam = triggerQueue.poll();
116
 			TriggerParam triggerParam = triggerQueue.poll();
114
 			if (triggerParam!=null) {
117
 			if (triggerParam!=null) {
115
 				// is killed
118
 				// is killed
116
-				TriggerCallbackThread.pushCallBack(new HandleCallbackParam(triggerParam.getLogId(), triggerParam.getLogAddress(), ReturnT.FAIL_CODE, stopReason + " [任务尚未执行,在调度队列中被终止]"));
119
+				ReturnT stopResult = new ReturnT<String>(ReturnT.FAIL_CODE, stopReason + " [任务尚未执行,在调度队列中被终止]");
120
+				TriggerCallbackThread.pushCallBack(new HandleCallbackParam(triggerParam.getLogId(), triggerParam.getLogAddress(), stopResult));
117
 			}
121
 			}
118
 		}
122
 		}
119
 		
123
 		

+ 3 - 1
xxl-job-executor-example/src/main/java/com/xxl/job/executor/service/jobhandler/DemoJobHandler.java 查看文件

1
 package com.xxl.job.executor.service.jobhandler;
1
 package com.xxl.job.executor.service.jobhandler;
2
 
2
 
3
+import com.xxl.job.core.biz.model.ReturnT;
3
 import com.xxl.job.core.handler.IJobHandler;
4
 import com.xxl.job.core.handler.IJobHandler;
4
 import com.xxl.job.core.handler.annotation.JobHander;
5
 import com.xxl.job.core.handler.annotation.JobHander;
5
 import org.slf4j.Logger;
6
 import org.slf4j.Logger;
25
 	private static transient Logger logger = LoggerFactory.getLogger(DemoJobHandler.class);
26
 	private static transient Logger logger = LoggerFactory.getLogger(DemoJobHandler.class);
26
 	
27
 	
27
 	@Override
28
 	@Override
28
-	public void execute(String... params) throws Exception {
29
+	public ReturnT<String> execute(String... params) throws Exception {
29
 		logger.info("XXL-JOB, Hello World.");
30
 		logger.info("XXL-JOB, Hello World.");
30
 		
31
 		
31
 		for (int i = 0; i < 5; i++) {
32
 		for (int i = 0; i < 5; i++) {
32
 			logger.info("beat at:{}", i);
33
 			logger.info("beat at:{}", i);
33
 			TimeUnit.SECONDS.sleep(2);
34
 			TimeUnit.SECONDS.sleep(2);
34
 		}
35
 		}
36
+		return ReturnT.SUCCESS;
35
 	}
37
 	}
36
 	
38
 	
37
 }
39
 }