Procházet zdrojové kódy

JobHandler支持自定义回调结果;

xueli.xue před 8 roky
rodič
revize
b7b411ce2a

+ 3 - 1
README.md Zobrazit soubor

@@ -772,6 +772,7 @@ Tips: 历史版本(V1.3.x)目前已经Release至稳定版本, 进入维护阶段
772 772
 
773 773
 #### 6.12 版本 V1.6.2 特性(Coding)
774 774
 - 1、任务报表:总任务数、总调度数、调度成功比例;
775
+- 2、JobHandler支持自定义回调结果;
775 776
 
776 777
 #### TODO LIST
777 778
 - 1、支持脚本JOB(源码或指定路径), 即shell/python/php等, 日志实时输出并支持在线监控;定制JobHandler实现;
@@ -779,7 +780,8 @@ Tips: 历史版本(V1.3.x)目前已经Release至稳定版本, 进入维护阶段
779 780
 - 3、任务权限管理;
780 781
 - 4、执行器,server启动,注册逻辑调整;
781 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 Zobrazit soubor

@@ -120,12 +120,10 @@ public class JobLogController {
120 120
 			ReturnT<LogResult> logResult = executorBiz.log(triggerTime, logId, fromLineNum);
121 121
 
122 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 128
 			return logResult;
131 129
 		} catch (Exception e) {

+ 4 - 4
xxl-job-admin/src/main/java/com/xxl/job/admin/core/biz/AdminBizImpl.java Zobrazit soubor

@@ -31,7 +31,7 @@ public class AdminBizImpl implements AdminBiz {
31 31
 
32 32
         // trigger success, to trigger child job, and avoid repeat trigger child job
33 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 35
             XxlJobInfo xxlJobInfo = XxlJobDynamicScheduler.xxlJobInfoDao.loadById(log.getJobId());
36 36
             if (xxlJobInfo!=null && StringUtils.isNotBlank(xxlJobInfo.getChildJobKey())) {
37 37
                 childTriggerMsg = "<hr>";
@@ -68,8 +68,8 @@ public class AdminBizImpl implements AdminBiz {
68 68
         if (log.getHandleMsg()!=null) {
69 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 74
         if (childTriggerMsg !=null) {
75 75
             handleMsg.append("<br>子任务触发备注:").append(childTriggerMsg);
@@ -77,7 +77,7 @@ public class AdminBizImpl implements AdminBiz {
77 77
 
78 78
         // success, save log
79 79
         log.setHandleTime(new Date());
80
-        log.setHandleCode(handleCallbackParam.getCode());
80
+        log.setHandleCode(handleCallbackParam.getExecuteResult().getCode());
81 81
         log.setHandleMsg(handleMsg.toString());
82 82
         XxlJobDynamicScheduler.xxlJobLogDao.updateHandleInfo(log);
83 83
 

+ 3 - 1
xxl-job-admin/src/main/webapp/WEB-INF/template/jobinfo/jobinfo.index.ftl Zobrazit soubor

@@ -172,14 +172,16 @@ package com.xxl.job.service.handler;
172 172
 import org.slf4j.Logger;
173 173
 import org.slf4j.LoggerFactory;
174 174
 
175
+import com.xxl.job.core.biz.model.ReturnT;
175 176
 import com.xxl.job.core.handler.IJobHandler;
176 177
 
177 178
 public class DemoGlueJobHandler extends IJobHandler {
178 179
 	private static transient Logger logger = LoggerFactory.getLogger(DemoGlueJobHandler.class);
179 180
 
180 181
 	@Override
181
-	public void execute(String... params) throws Exception {
182
+	public ReturnT<String> execute(String... params) throws Exception {
182 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 Zobrazit soubor

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

+ 7 - 18
xxl-job-core/src/main/java/com/xxl/job/core/biz/model/HandleCallbackParam.java Zobrazit soubor

@@ -12,14 +12,12 @@ public class HandleCallbackParam implements Serializable {
12 12
     private int logId;
13 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 18
         this.logId = logId;
20 19
         this.logAddress = logAddress;
21
-        this.code = code;
22
-        this.msg = msg;
20
+        this.executeResult = executeResult;
23 21
     }
24 22
 
25 23
     public int getLogId() {
@@ -38,20 +36,11 @@ public class HandleCallbackParam implements Serializable {
38 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 Zobrazit soubor

@@ -1,5 +1,7 @@
1 1
 package com.xxl.job.core.handler;
2 2
 
3
+import com.xxl.job.core.biz.model.ReturnT;
4
+
3 5
 /**
4 6
  * remote job handler
5 7
  * @author xuxueli 2015-12-19 19:06:38
@@ -7,11 +9,11 @@ package com.xxl.job.core.handler;
7 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 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 Zobrazit soubor

@@ -1,5 +1,6 @@
1 1
 package com.xxl.job.core.handler.impl;
2 2
 
3
+import com.xxl.job.core.biz.model.ReturnT;
3 4
 import com.xxl.job.core.handler.IJobHandler;
4 5
 import org.slf4j.Logger;
5 6
 import org.slf4j.LoggerFactory;
@@ -22,9 +23,10 @@ public class GlueJobHandler extends IJobHandler {
22 23
 	}
23 24
 
24 25
 	@Override
25
-	public void execute(String... params) throws Exception {
26
+	public ReturnT<String> execute(String... params) throws Exception {
26 27
 		logger.info("----------- glue.version:{} -----------", glueUpdatetime);
27 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 Zobrazit soubor

@@ -122,12 +122,12 @@ public class XxlJobFileAppender extends AppenderSkeleton {
122 122
 
123 123
 		// valid log file
124 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 127
 		File logFile = new File(filePath, logFileName);
128 128
 
129 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 133
 		// read file
@@ -139,8 +139,8 @@ public class XxlJobFileAppender extends AppenderSkeleton {
139 139
 			String line = null;
140 140
 
141 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 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 Zobrazit soubor

@@ -74,33 +74,36 @@ public class JobThread extends Thread{
74 74
 							? (String[])(Arrays.asList(triggerParam.getExecutorParams().split(",")).toArray()) : null;
75 75
 					
76 76
 					// handle job
77
-					int _code = ReturnT.SUCCESS_CODE;
78
-					String _msg = null;
79
-
77
+					ReturnT<String> executeResult = null;
80 78
 					try {
81 79
 						// log filename: yyyy-MM-dd/9999.log
82 80
 						String logFileName = XxlJobFileAppender.makeLogFileName(new Date(triggerParam.getLogDateTim()), triggerParam.getLogId());
83 81
 
84 82
 						XxlJobFileAppender.contextHolder.set(logFileName);
85 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 89
 					} catch (Exception e) {
88 90
 						logger.error("JobThread Exception:", e);
89
-						_code = ReturnT.FAIL_CODE;
90 91
 						StringWriter out = new StringWriter();
91 92
 						e.printStackTrace(new PrintWriter(out));
92
-						_msg = out.toString();
93
+
94
+						executeResult = new ReturnT<String>(ReturnT.FAIL_CODE, out.toString());
93 95
 					}
94 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 99
 					// callback handler info
98 100
 					if (!toStop) {
99 101
 						// commonm
100
-						TriggerCallbackThread.pushCallBack(new HandleCallbackParam(triggerParam.getLogId(), triggerParam.getLogAddress(), _code, _msg));
102
+						TriggerCallbackThread.pushCallBack(new HandleCallbackParam(triggerParam.getLogId(), triggerParam.getLogAddress(), executeResult));
101 103
 					} else {
102 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 109
 			} catch (Exception e) {
@@ -113,7 +116,8 @@ public class JobThread extends Thread{
113 116
 			TriggerParam triggerParam = triggerQueue.poll();
114 117
 			if (triggerParam!=null) {
115 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 Zobrazit soubor

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