|
@@ -9,29 +9,33 @@ import com.xxl.job.admin.dao.XxlJobInfoDao;
|
9
|
9
|
import com.xxl.job.admin.dao.XxlJobLogDao;
|
10
|
10
|
import com.xxl.job.admin.dao.XxlJobRegistryDao;
|
11
|
11
|
import com.xxl.job.core.biz.AdminBiz;
|
|
12
|
+import com.xxl.job.core.biz.ExecutorBiz;
|
|
13
|
+import com.xxl.job.core.rpc.netcom.NetComClientProxy;
|
12
|
14
|
import com.xxl.job.core.rpc.netcom.NetComServerFactory;
|
13
|
15
|
import org.quartz.*;
|
14
|
16
|
import org.quartz.Trigger.TriggerState;
|
15
|
|
-import org.quartz.impl.matchers.GroupMatcher;
|
16
|
17
|
import org.quartz.impl.triggers.CronTriggerImpl;
|
17
|
18
|
import org.slf4j.Logger;
|
18
|
19
|
import org.slf4j.LoggerFactory;
|
19
|
20
|
import org.springframework.beans.BeansException;
|
20
|
|
-import org.springframework.beans.factory.InitializingBean;
|
21
|
21
|
import org.springframework.context.ApplicationContext;
|
22
|
22
|
import org.springframework.context.ApplicationContextAware;
|
23
|
23
|
import org.springframework.util.Assert;
|
24
|
24
|
|
25
|
|
-import java.util.*;
|
|
25
|
+import java.util.Date;
|
|
26
|
+import java.util.HashSet;
|
|
27
|
+import java.util.concurrent.ConcurrentHashMap;
|
26
|
28
|
|
27
|
29
|
/**
|
28
|
30
|
* base quartz scheduler util
|
29
|
31
|
* @author xuxueli 2015-12-19 16:13:53
|
30
|
32
|
*/
|
31
|
|
-public final class XxlJobDynamicScheduler implements ApplicationContextAware, InitializingBean {
|
|
33
|
+public final class XxlJobDynamicScheduler implements ApplicationContextAware {
|
32
|
34
|
private static final Logger logger = LoggerFactory.getLogger(XxlJobDynamicScheduler.class);
|
33
|
|
-
|
34
|
|
- // Scheduler
|
|
35
|
+
|
|
36
|
+ // ---------------------- param ----------------------
|
|
37
|
+
|
|
38
|
+ // scheduler
|
35
|
39
|
private static Scheduler scheduler;
|
36
|
40
|
public void setScheduler(Scheduler scheduler) {
|
37
|
41
|
XxlJobDynamicScheduler.scheduler = scheduler;
|
|
@@ -42,25 +46,41 @@ public final class XxlJobDynamicScheduler implements ApplicationContextAware, In
|
42
|
46
|
public void setAccessToken(String accessToken) {
|
43
|
47
|
this.accessToken = accessToken;
|
44
|
48
|
}
|
45
|
|
- public static String getAccessToken() {
|
46
|
|
- return accessToken;
|
47
|
|
- }
|
48
|
49
|
|
49
|
|
- // init
|
|
50
|
+ // dao
|
|
51
|
+ public static XxlJobLogDao xxlJobLogDao;
|
|
52
|
+ public static XxlJobInfoDao xxlJobInfoDao;
|
|
53
|
+ public static XxlJobRegistryDao xxlJobRegistryDao;
|
|
54
|
+ public static XxlJobGroupDao xxlJobGroupDao;
|
|
55
|
+ public static AdminBiz adminBiz;
|
|
56
|
+
|
|
57
|
+ // ---------------------- applicationContext ----------------------
|
|
58
|
+ @Override
|
|
59
|
+ public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
60
|
+ XxlJobDynamicScheduler.xxlJobLogDao = applicationContext.getBean(XxlJobLogDao.class);
|
|
61
|
+ XxlJobDynamicScheduler.xxlJobInfoDao = applicationContext.getBean(XxlJobInfoDao.class);
|
|
62
|
+ XxlJobDynamicScheduler.xxlJobRegistryDao = applicationContext.getBean(XxlJobRegistryDao.class);
|
|
63
|
+ XxlJobDynamicScheduler.xxlJobGroupDao = applicationContext.getBean(XxlJobGroupDao.class);
|
|
64
|
+ XxlJobDynamicScheduler.adminBiz = applicationContext.getBean(AdminBiz.class);
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ // ---------------------- init + destroy ----------------------
|
50
|
68
|
public void init() throws Exception {
|
51
|
|
- // admin registry monitor run
|
|
69
|
+ // admin registry monitor run
|
52
|
70
|
JobRegistryMonitorHelper.getInstance().start();
|
53
|
71
|
|
54
|
72
|
// admin monitor run
|
55
|
73
|
JobFailMonitorHelper.getInstance().start();
|
56
|
74
|
|
57
|
|
- // rpc-service, base on spring-mvc
|
|
75
|
+ // admin-server(spring-mvc)
|
58
|
76
|
NetComServerFactory.putService(AdminBiz.class, XxlJobDynamicScheduler.adminBiz);
|
59
|
77
|
NetComServerFactory.setAccessToken(accessToken);
|
60
|
78
|
|
|
79
|
+ // valid
|
|
80
|
+ Assert.notNull(scheduler, "quartz scheduler is null");
|
|
81
|
+ logger.info(">>>>>>>>> init quartz scheduler success.[{}]", scheduler);
|
61
|
82
|
}
|
62
|
|
-
|
63
|
|
- // destroy
|
|
83
|
+
|
64
|
84
|
public void destroy(){
|
65
|
85
|
// admin registry stop
|
66
|
86
|
JobRegistryMonitorHelper.getInstance().toStop();
|
|
@@ -68,64 +88,35 @@ public final class XxlJobDynamicScheduler implements ApplicationContextAware, In
|
68
|
88
|
// admin monitor stop
|
69
|
89
|
JobFailMonitorHelper.getInstance().toStop();
|
70
|
90
|
}
|
71
|
|
-
|
72
|
|
- // xxlJobLogDao、xxlJobInfoDao
|
73
|
|
- public static XxlJobLogDao xxlJobLogDao;
|
74
|
|
- public static XxlJobInfoDao xxlJobInfoDao;
|
75
|
|
- public static XxlJobRegistryDao xxlJobRegistryDao;
|
76
|
|
- public static XxlJobGroupDao xxlJobGroupDao;
|
77
|
|
- public static AdminBiz adminBiz;
|
78
|
91
|
|
79
|
|
- @Override
|
80
|
|
- public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
81
|
|
- XxlJobDynamicScheduler.xxlJobLogDao = applicationContext.getBean(XxlJobLogDao.class);
|
82
|
|
- XxlJobDynamicScheduler.xxlJobInfoDao = applicationContext.getBean(XxlJobInfoDao.class);
|
83
|
|
- XxlJobDynamicScheduler.xxlJobRegistryDao = applicationContext.getBean(XxlJobRegistryDao.class);
|
84
|
|
- XxlJobDynamicScheduler.xxlJobGroupDao = applicationContext.getBean(XxlJobGroupDao.class);
|
85
|
|
- XxlJobDynamicScheduler.adminBiz = applicationContext.getBean(AdminBiz.class);
|
86
|
|
- }
|
87
|
|
-
|
88
|
|
- @Override
|
89
|
|
- public void afterPropertiesSet() throws Exception {
|
90
|
|
- Assert.notNull(scheduler, "quartz scheduler is null");
|
91
|
|
- logger.info(">>>>>>>>> init quartz scheduler success.[{}]", scheduler);
|
92
|
|
-
|
|
92
|
+ // ---------------------- executor-client ----------------------
|
|
93
|
+ private static ConcurrentHashMap<String, ExecutorBiz> executorBizRepository = new ConcurrentHashMap<String, ExecutorBiz>();
|
|
94
|
+ public static ExecutorBiz getExecutorBiz(String address) throws Exception {
|
|
95
|
+ // valid
|
|
96
|
+ if (address==null || address.trim().length()==0) {
|
|
97
|
+ return null;
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+ // load-cache
|
|
101
|
+ address = address.trim();
|
|
102
|
+ ExecutorBiz executorBiz = executorBizRepository.get(address);
|
|
103
|
+ if (executorBiz != null) {
|
|
104
|
+ return executorBiz;
|
|
105
|
+ }
|
|
106
|
+
|
|
107
|
+ // set-cache
|
|
108
|
+ executorBiz = (ExecutorBiz) new NetComClientProxy(ExecutorBiz.class, address, accessToken).getObject();
|
|
109
|
+ executorBizRepository.put(address, executorBiz);
|
|
110
|
+ return executorBiz;
|
93
|
111
|
}
|
94
|
|
-
|
95
|
|
- // getJobKeys
|
96
|
|
- @Deprecated
|
97
|
|
- public static List<Map<String, Object>> getJobList(){
|
98
|
|
- List<Map<String, Object>> jobList = new ArrayList<Map<String,Object>>();
|
99
|
|
-
|
100
|
|
- try {
|
101
|
|
- if (scheduler.getJobGroupNames()==null || scheduler.getJobGroupNames().size()==0) {
|
102
|
|
- return null;
|
103
|
|
- }
|
104
|
|
- String groupName = scheduler.getJobGroupNames().get(0);
|
105
|
|
- Set<JobKey> jobKeys = scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName));
|
106
|
|
- if (jobKeys!=null && jobKeys.size()>0) {
|
107
|
|
- for (JobKey jobKey : jobKeys) {
|
108
|
|
- TriggerKey triggerKey = TriggerKey.triggerKey(jobKey.getName(), Scheduler.DEFAULT_GROUP);
|
109
|
|
- Trigger trigger = scheduler.getTrigger(triggerKey);
|
110
|
|
- JobDetail jobDetail = scheduler.getJobDetail(jobKey);
|
111
|
|
- TriggerState triggerState = scheduler.getTriggerState(triggerKey);
|
112
|
|
- Map<String, Object> jobMap = new HashMap<String, Object>();
|
113
|
|
- jobMap.put("TriggerKey", triggerKey);
|
114
|
|
- jobMap.put("Trigger", trigger);
|
115
|
|
- jobMap.put("JobDetail", jobDetail);
|
116
|
|
- jobMap.put("TriggerState", triggerState);
|
117
|
|
- jobList.add(jobMap);
|
118
|
|
- }
|
119
|
|
- }
|
120
|
|
-
|
121
|
|
- } catch (SchedulerException e) {
|
122
|
|
- e.printStackTrace();
|
123
|
|
- return null;
|
124
|
|
- }
|
125
|
|
- return jobList;
|
126
|
|
- }
|
127
|
|
-
|
128
|
|
- // fill job info
|
|
112
|
+
|
|
113
|
+ // ---------------------- schedule util ----------------------
|
|
114
|
+
|
|
115
|
+ /**
|
|
116
|
+ * fill job info
|
|
117
|
+ *
|
|
118
|
+ * @param jobInfo
|
|
119
|
+ */
|
129
|
120
|
public static void fillJobInfo(XxlJobInfo jobInfo) {
|
130
|
121
|
// TriggerKey : name + group
|
131
|
122
|
String group = String.valueOf(jobInfo.getJobGroup());
|
|
@@ -156,14 +147,28 @@ public final class XxlJobDynamicScheduler implements ApplicationContextAware, In
|
156
|
147
|
}
|
157
|
148
|
}
|
158
|
149
|
|
159
|
|
- // check if exists
|
|
150
|
+ /**
|
|
151
|
+ * check if exists
|
|
152
|
+ *
|
|
153
|
+ * @param jobName
|
|
154
|
+ * @param jobGroup
|
|
155
|
+ * @return
|
|
156
|
+ * @throws SchedulerException
|
|
157
|
+ */
|
160
|
158
|
public static boolean checkExists(String jobName, String jobGroup) throws SchedulerException{
|
161
|
159
|
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
|
162
|
160
|
return scheduler.checkExists(triggerKey);
|
163
|
161
|
}
|
164
|
162
|
|
165
|
|
- // addJob 新增
|
166
|
|
- @SuppressWarnings("unchecked")
|
|
163
|
+ /**
|
|
164
|
+ * addJob
|
|
165
|
+ *
|
|
166
|
+ * @param jobName
|
|
167
|
+ * @param jobGroup
|
|
168
|
+ * @param cronExpression
|
|
169
|
+ * @return
|
|
170
|
+ * @throws SchedulerException
|
|
171
|
+ */
|
167
|
172
|
public static boolean addJob(String jobName, String jobGroup, String cronExpression) throws SchedulerException {
|
168
|
173
|
// TriggerKey : name + group
|
169
|
174
|
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
|
|
@@ -196,7 +201,15 @@ public final class XxlJobDynamicScheduler implements ApplicationContextAware, In
|
196
|
201
|
return true;
|
197
|
202
|
}
|
198
|
203
|
|
199
|
|
- // reschedule
|
|
204
|
+ /**
|
|
205
|
+ * rescheduleJob
|
|
206
|
+ *
|
|
207
|
+ * @param jobGroup
|
|
208
|
+ * @param jobName
|
|
209
|
+ * @param cronExpression
|
|
210
|
+ * @return
|
|
211
|
+ * @throws SchedulerException
|
|
212
|
+ */
|
200
|
213
|
public static boolean rescheduleJob(String jobGroup, String jobName, String cronExpression) throws SchedulerException {
|
201
|
214
|
|
202
|
215
|
// TriggerKey valid if_exists
|
|
@@ -245,7 +258,14 @@ public final class XxlJobDynamicScheduler implements ApplicationContextAware, In
|
245
|
258
|
return true;
|
246
|
259
|
}
|
247
|
260
|
|
248
|
|
- // unscheduleJob
|
|
261
|
+ /**
|
|
262
|
+ * unscheduleJob
|
|
263
|
+ *
|
|
264
|
+ * @param jobName
|
|
265
|
+ * @param jobGroup
|
|
266
|
+ * @return
|
|
267
|
+ * @throws SchedulerException
|
|
268
|
+ */
|
249
|
269
|
public static boolean removeJob(String jobName, String jobGroup) throws SchedulerException {
|
250
|
270
|
// TriggerKey : name + group
|
251
|
271
|
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
|
|
@@ -257,7 +277,14 @@ public final class XxlJobDynamicScheduler implements ApplicationContextAware, In
|
257
|
277
|
return true;
|
258
|
278
|
}
|
259
|
279
|
|
260
|
|
- // Pause
|
|
280
|
+ /**
|
|
281
|
+ * pause
|
|
282
|
+ *
|
|
283
|
+ * @param jobName
|
|
284
|
+ * @param jobGroup
|
|
285
|
+ * @return
|
|
286
|
+ * @throws SchedulerException
|
|
287
|
+ */
|
261
|
288
|
public static boolean pauseJob(String jobName, String jobGroup) throws SchedulerException {
|
262
|
289
|
// TriggerKey : name + group
|
263
|
290
|
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
|
|
@@ -273,7 +300,14 @@ public final class XxlJobDynamicScheduler implements ApplicationContextAware, In
|
273
|
300
|
return result;
|
274
|
301
|
}
|
275
|
302
|
|
276
|
|
- // resume
|
|
303
|
+ /**
|
|
304
|
+ * resume
|
|
305
|
+ *
|
|
306
|
+ * @param jobName
|
|
307
|
+ * @param jobGroup
|
|
308
|
+ * @return
|
|
309
|
+ * @throws SchedulerException
|
|
310
|
+ */
|
277
|
311
|
public static boolean resumeJob(String jobName, String jobGroup) throws SchedulerException {
|
278
|
312
|
// TriggerKey : name + group
|
279
|
313
|
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);
|
|
@@ -289,7 +323,14 @@ public final class XxlJobDynamicScheduler implements ApplicationContextAware, In
|
289
|
323
|
return result;
|
290
|
324
|
}
|
291
|
325
|
|
292
|
|
- // run
|
|
326
|
+ /**
|
|
327
|
+ * run
|
|
328
|
+ *
|
|
329
|
+ * @param jobName
|
|
330
|
+ * @param jobGroup
|
|
331
|
+ * @return
|
|
332
|
+ * @throws SchedulerException
|
|
333
|
+ */
|
293
|
334
|
public static boolean triggerJob(String jobName, String jobGroup) throws SchedulerException {
|
294
|
335
|
// TriggerKey : name + group
|
295
|
336
|
JobKey jobKey = new JobKey(jobName, jobGroup);
|
|
@@ -305,5 +346,41 @@ public final class XxlJobDynamicScheduler implements ApplicationContextAware, In
|
305
|
346
|
return result;
|
306
|
347
|
}
|
307
|
348
|
|
|
349
|
+ /**
|
|
350
|
+ * finaAllJobList
|
|
351
|
+ *
|
|
352
|
+ * @return
|
|
353
|
+ *//*
|
|
354
|
+ @Deprecated
|
|
355
|
+ public static List<Map<String, Object>> finaAllJobList(){
|
|
356
|
+ List<Map<String, Object>> jobList = new ArrayList<Map<String,Object>>();
|
|
357
|
+
|
|
358
|
+ try {
|
|
359
|
+ if (scheduler.getJobGroupNames()==null || scheduler.getJobGroupNames().size()==0) {
|
|
360
|
+ return null;
|
|
361
|
+ }
|
|
362
|
+ String groupName = scheduler.getJobGroupNames().get(0);
|
|
363
|
+ Set<JobKey> jobKeys = scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName));
|
|
364
|
+ if (jobKeys!=null && jobKeys.size()>0) {
|
|
365
|
+ for (JobKey jobKey : jobKeys) {
|
|
366
|
+ TriggerKey triggerKey = TriggerKey.triggerKey(jobKey.getName(), Scheduler.DEFAULT_GROUP);
|
|
367
|
+ Trigger trigger = scheduler.getTrigger(triggerKey);
|
|
368
|
+ JobDetail jobDetail = scheduler.getJobDetail(jobKey);
|
|
369
|
+ TriggerState triggerState = scheduler.getTriggerState(triggerKey);
|
|
370
|
+ Map<String, Object> jobMap = new HashMap<String, Object>();
|
|
371
|
+ jobMap.put("TriggerKey", triggerKey);
|
|
372
|
+ jobMap.put("Trigger", trigger);
|
|
373
|
+ jobMap.put("JobDetail", jobDetail);
|
|
374
|
+ jobMap.put("TriggerState", triggerState);
|
|
375
|
+ jobList.add(jobMap);
|
|
376
|
+ }
|
|
377
|
+ }
|
|
378
|
+
|
|
379
|
+ } catch (SchedulerException e) {
|
|
380
|
+ e.printStackTrace();
|
|
381
|
+ return null;
|
|
382
|
+ }
|
|
383
|
+ return jobList;
|
|
384
|
+ }*/
|
308
|
385
|
|
309
|
386
|
}
|