|
@@ -3,6 +3,7 @@ package com.xxl.job.core.thread;
|
3
|
3
|
import com.xxl.job.core.biz.model.HandleCallbackParam;
|
4
|
4
|
import com.xxl.job.core.biz.model.ReturnT;
|
5
|
5
|
import com.xxl.job.core.biz.model.TriggerParam;
|
|
6
|
+import com.xxl.job.core.executor.XxlJobExecutor;
|
6
|
7
|
import com.xxl.job.core.handler.IJobHandler;
|
7
|
8
|
import com.xxl.job.core.log.XxlJobFileAppender;
|
8
|
9
|
import com.xxl.job.core.log.XxlJobLogger;
|
|
@@ -23,7 +24,8 @@ import java.util.concurrent.TimeUnit;
|
23
|
24
|
*/
|
24
|
25
|
public class JobThread extends Thread{
|
25
|
26
|
private static Logger logger = LoggerFactory.getLogger(JobThread.class);
|
26
|
|
-
|
|
27
|
+
|
|
28
|
+ private int jobId;
|
27
|
29
|
private IJobHandler handler;
|
28
|
30
|
private LinkedBlockingQueue<TriggerParam> triggerQueue;
|
29
|
31
|
private ConcurrentHashSet<Integer> triggerLogIdSet; // avoid repeat trigger for the same TRIGGER_LOG_ID
|
|
@@ -32,12 +34,14 @@ public class JobThread extends Thread{
|
32
|
34
|
private String stopReason;
|
33
|
35
|
|
34
|
36
|
private boolean running = false; // if running job
|
|
37
|
+ private int idleTimes = 0; // idel times
|
35
|
38
|
|
36
|
39
|
|
37
|
|
- public JobThread(IJobHandler handler) {
|
|
40
|
+ public JobThread(int jobId, IJobHandler handler) {
|
|
41
|
+ this.jobId = jobId;
|
38
|
42
|
this.handler = handler;
|
39
|
|
- triggerQueue = new LinkedBlockingQueue<TriggerParam>();
|
40
|
|
- triggerLogIdSet = new ConcurrentHashSet<Integer>();
|
|
43
|
+ this.triggerQueue = new LinkedBlockingQueue<TriggerParam>();
|
|
44
|
+ this.triggerLogIdSet = new ConcurrentHashSet<Integer>();
|
41
|
45
|
}
|
42
|
46
|
public IJobHandler getHandler() {
|
43
|
47
|
return handler;
|
|
@@ -88,11 +92,13 @@ public class JobThread extends Thread{
|
88
|
92
|
public void run() {
|
89
|
93
|
while(!toStop){
|
90
|
94
|
running = false;
|
|
95
|
+ idleTimes++;
|
91
|
96
|
try {
|
92
|
97
|
// to check toStop signal, we need cycle, so wo cannot use queue.take(), instand of poll(timeout)
|
93
|
98
|
TriggerParam triggerParam = triggerQueue.poll(3L, TimeUnit.SECONDS);
|
94
|
99
|
if (triggerParam!=null) {
|
95
|
100
|
running = true;
|
|
101
|
+ idleTimes = 0;
|
96
|
102
|
triggerLogIdSet.remove(triggerParam.getLogId());
|
97
|
103
|
|
98
|
104
|
// parse param
|
|
@@ -126,9 +132,6 @@ public class JobThread extends Thread{
|
126
|
132
|
|
127
|
133
|
XxlJobLogger.log("<br>----------- JobThread Exception:" + errorMsg + "<br>----------- xxl-job job execute end(error) -----------");
|
128
|
134
|
}
|
129
|
|
-
|
130
|
|
-
|
131
|
|
-
|
132
|
135
|
|
133
|
136
|
// callback handler info
|
134
|
137
|
if (!toStop) {
|
|
@@ -139,8 +142,12 @@ public class JobThread extends Thread{
|
139
|
142
|
ReturnT<String> stopResult = new ReturnT<String>(ReturnT.FAIL_CODE, stopReason + " [业务运行中,被强制终止]");
|
140
|
143
|
TriggerCallbackThread.pushCallBack(new HandleCallbackParam(triggerParam.getLogId(), stopResult));
|
141
|
144
|
}
|
|
145
|
+ } else {
|
|
146
|
+ if (idleTimes > 3) {
|
|
147
|
+ XxlJobExecutor.removeJobThread(jobId, "excutor idel times over limit.");
|
|
148
|
+ }
|
142
|
149
|
}
|
143
|
|
- } catch (Exception e) {
|
|
150
|
+ } catch (Throwable e) {
|
144
|
151
|
if (toStop) {
|
145
|
152
|
XxlJobLogger.log("<br>----------- xxl-job toStop, stopReason:" + stopReason);
|
146
|
153
|
}
|