|
@@ -5,10 +5,9 @@ import com.xxl.job.admin.core.trigger.XxlJobTrigger;
|
5
|
5
|
import org.slf4j.Logger;
|
6
|
6
|
import org.slf4j.LoggerFactory;
|
7
|
7
|
|
8
|
|
-import java.util.concurrent.LinkedBlockingQueue;
|
9
|
|
-import java.util.concurrent.ThreadFactory;
|
10
|
|
-import java.util.concurrent.ThreadPoolExecutor;
|
11
|
|
-import java.util.concurrent.TimeUnit;
|
|
8
|
+import java.util.Map;
|
|
9
|
+import java.util.concurrent.*;
|
|
10
|
+import java.util.concurrent.atomic.AtomicInteger;
|
12
|
11
|
|
13
|
12
|
/**
|
14
|
13
|
* job trigger thread pool helper
|
|
@@ -21,32 +20,91 @@ public class JobTriggerPoolHelper {
|
21
|
20
|
|
22
|
21
|
// ---------------------- trigger pool ----------------------
|
23
|
22
|
|
24
|
|
- private ThreadPoolExecutor triggerPool = new ThreadPoolExecutor(
|
25
|
|
- 32,
|
26
|
|
- 256,
|
|
23
|
+ // fast/slow thread pool
|
|
24
|
+ private ThreadPoolExecutor fastTriggerPool = new ThreadPoolExecutor(
|
|
25
|
+ 8,
|
|
26
|
+ 200,
|
27
|
27
|
60L,
|
28
|
28
|
TimeUnit.SECONDS,
|
29
|
29
|
new LinkedBlockingQueue<Runnable>(1000),
|
30
|
30
|
new ThreadFactory() {
|
31
|
31
|
@Override
|
32
|
32
|
public Thread newThread(Runnable r) {
|
33
|
|
- return new Thread(r, "xxl-job, admin JobTriggerPoolHelper-triggerPool-" + r.hashCode());
|
|
33
|
+ return new Thread(r, "xxl-job, admin JobTriggerPoolHelper-fastTriggerPool-" + r.hashCode());
|
34
|
34
|
}
|
35
|
35
|
});
|
36
|
36
|
|
|
37
|
+ private ThreadPoolExecutor slowTriggerPool = new ThreadPoolExecutor(
|
|
38
|
+ 0,
|
|
39
|
+ 100,
|
|
40
|
+ 60L,
|
|
41
|
+ TimeUnit.SECONDS,
|
|
42
|
+ new LinkedBlockingQueue<Runnable>(2000),
|
|
43
|
+ new ThreadFactory() {
|
|
44
|
+ @Override
|
|
45
|
+ public Thread newThread(Runnable r) {
|
|
46
|
+ return new Thread(r, "xxl-job, admin JobTriggerPoolHelper-slowTriggerPool-" + r.hashCode());
|
|
47
|
+ }
|
|
48
|
+ });
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+ // job timeout count
|
|
52
|
+ private volatile long minTim = System.currentTimeMillis()/60000; // ms > min
|
|
53
|
+ private volatile Map<Integer, AtomicInteger> jobTimeoutCountMap = new ConcurrentHashMap<>();
|
37
|
54
|
|
|
55
|
+
|
|
56
|
+ /**
|
|
57
|
+ * add trigger
|
|
58
|
+ */
|
38
|
59
|
public void addTrigger(final int jobId, final TriggerTypeEnum triggerType, final int failRetryCount, final String executorShardingParam, final String executorParam) {
|
39
|
|
- triggerPool.execute(new Runnable() {
|
|
60
|
+
|
|
61
|
+ // choose thread pool
|
|
62
|
+ ThreadPoolExecutor triggerPool_ = fastTriggerPool;
|
|
63
|
+ AtomicInteger jobTimeoutCount = jobTimeoutCountMap.get(jobId);
|
|
64
|
+ if (jobTimeoutCount!=null && jobTimeoutCount.get() > 10) { // job-timeout 10 times in 1 min
|
|
65
|
+ triggerPool_ = slowTriggerPool;
|
|
66
|
+ }
|
|
67
|
+
|
|
68
|
+ // trigger
|
|
69
|
+ triggerPool_.execute(new Runnable() {
|
40
|
70
|
@Override
|
41
|
71
|
public void run() {
|
42
|
|
- XxlJobTrigger.trigger(jobId, triggerType, failRetryCount, executorShardingParam, executorParam);
|
|
72
|
+
|
|
73
|
+ long start = System.currentTimeMillis();
|
|
74
|
+
|
|
75
|
+ try {
|
|
76
|
+ // do trigger
|
|
77
|
+ XxlJobTrigger.trigger(jobId, triggerType, failRetryCount, executorShardingParam, executorParam);
|
|
78
|
+ } catch (Exception e) {
|
|
79
|
+ logger.error(e.getMessage(), e);
|
|
80
|
+ } finally {
|
|
81
|
+
|
|
82
|
+ // check timeout-count-map
|
|
83
|
+ long minTim_now = System.currentTimeMillis()/60000;
|
|
84
|
+ if (minTim != minTim_now) {
|
|
85
|
+ minTim = minTim_now;
|
|
86
|
+ jobTimeoutCountMap.clear();
|
|
87
|
+ }
|
|
88
|
+
|
|
89
|
+ // incr timeout-count-map
|
|
90
|
+ long cost = System.currentTimeMillis()-start;
|
|
91
|
+ if (cost > 500) { // ob-timeout threshold 500ms
|
|
92
|
+ AtomicInteger timeoutCount = jobTimeoutCountMap.put(jobId, new AtomicInteger(1));
|
|
93
|
+ if (timeoutCount != null) {
|
|
94
|
+ timeoutCount.incrementAndGet();
|
|
95
|
+ }
|
|
96
|
+ }
|
|
97
|
+
|
|
98
|
+ }
|
|
99
|
+
|
43
|
100
|
}
|
44
|
101
|
});
|
45
|
102
|
}
|
46
|
103
|
|
47
|
104
|
public void stop() {
|
48
|
105
|
//triggerPool.shutdown();
|
49
|
|
- triggerPool.shutdownNow();
|
|
106
|
+ fastTriggerPool.shutdownNow();
|
|
107
|
+ slowTriggerPool.shutdownNow();
|
50
|
108
|
logger.info(">>>>>>>>> xxl-job trigger thread pool shutdown success.");
|
51
|
109
|
}
|
52
|
110
|
|