|
@@ -26,7 +26,6 @@ import cn.com.qmth.examcloud.commons.base.logging.ExamCloudLog;
|
|
|
import cn.com.qmth.examcloud.commons.base.logging.ExamCloudLogFactory;
|
|
|
import cn.com.qmth.examcloud.commons.base.util.JsonUtil;
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* Quartz 管理器
|
|
|
*
|
|
@@ -35,8 +34,7 @@ import cn.com.qmth.examcloud.commons.base.util.JsonUtil;
|
|
|
* @Copyright (c) 2018-2020 WANGWEI [QQ:522080330] All Rights Reserved.
|
|
|
*/
|
|
|
@Component
|
|
|
-public class QuartzManager
|
|
|
-{
|
|
|
+public class QuartzManager {
|
|
|
private static final ExamCloudLog TASK_LOG = ExamCloudLogFactory.getLog("TASK_LOGGER");
|
|
|
|
|
|
@Autowired
|
|
@@ -48,22 +46,18 @@ public class QuartzManager
|
|
|
* @author WANGWEI
|
|
|
* @param job
|
|
|
*/
|
|
|
- public void addJob(ScheduleJob job)
|
|
|
- {
|
|
|
- if (TASK_LOG.isDebugEnabled())
|
|
|
- {
|
|
|
+ public void addJob(ScheduleJob job) {
|
|
|
+ if (TASK_LOG.isDebugEnabled()) {
|
|
|
TASK_LOG.debug("add a job. job detail: " + JsonUtil.toJson(job));
|
|
|
}
|
|
|
|
|
|
- try
|
|
|
- {
|
|
|
+ try {
|
|
|
Scheduler scheduler = schedulerFactoryBean.getScheduler();
|
|
|
TriggerKey triggerKey = TriggerKey.triggerKey(job.getJobName(), job.getJobGroup());
|
|
|
|
|
|
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
|
|
|
|
|
|
- if (null == trigger)
|
|
|
- {
|
|
|
+ if (null == trigger) {
|
|
|
Class<? extends Job> jobClass = job.isStateful()
|
|
|
? StatefulDistributionJob.class
|
|
|
: DistributionJob.class;
|
|
@@ -80,9 +74,7 @@ public class QuartzManager
|
|
|
.withSchedule(scheduleBuilder).build();
|
|
|
|
|
|
scheduler.scheduleJob(jobDetail, trigger);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
+ } else {
|
|
|
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder
|
|
|
.cronSchedule(job.getCronExpression());
|
|
|
|
|
@@ -91,15 +83,12 @@ public class QuartzManager
|
|
|
|
|
|
scheduler.rescheduleJob(triggerKey, trigger);
|
|
|
}
|
|
|
- }
|
|
|
- catch (SchedulerException e)
|
|
|
- {
|
|
|
+ } catch (SchedulerException e) {
|
|
|
TASK_LOG.error("Fail to add a job. job detail: " + JsonUtil.toJson(job), e);
|
|
|
throw new ExamCloudRuntimeException(e);
|
|
|
}
|
|
|
|
|
|
- if (TASK_LOG.isDebugEnabled())
|
|
|
- {
|
|
|
+ if (TASK_LOG.isDebugEnabled()) {
|
|
|
TASK_LOG.debug("add a job successfully. job detail: " + JsonUtil.toJson(job));
|
|
|
}
|
|
|
}
|
|
@@ -110,27 +99,21 @@ public class QuartzManager
|
|
|
* @author WANGWEI
|
|
|
* @return
|
|
|
*/
|
|
|
- public List<ScheduleJob> getAllJobs()
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
+ public List<ScheduleJob> getAllJobs() {
|
|
|
+ try {
|
|
|
Scheduler scheduler = schedulerFactoryBean.getScheduler();
|
|
|
GroupMatcher<JobKey> matcher = GroupMatcher.anyJobGroup();
|
|
|
Set<JobKey> jobKeys = scheduler.getJobKeys(matcher);
|
|
|
List<ScheduleJob> jobList = new ArrayList<ScheduleJob>();
|
|
|
- for (JobKey jobKey : jobKeys)
|
|
|
- {
|
|
|
+ for (JobKey jobKey : jobKeys) {
|
|
|
List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobKey);
|
|
|
- for (Trigger trigger : triggers)
|
|
|
- {
|
|
|
+ for (Trigger trigger : triggers) {
|
|
|
ScheduleJob job = new ScheduleJob();
|
|
|
job.setJobName(jobKey.getName());
|
|
|
job.setJobGroup(jobKey.getGroup());
|
|
|
- job.setDescription("触发器:" + trigger.getKey());
|
|
|
Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
|
|
|
- job.setJobStatus(triggerState.name());
|
|
|
- if (trigger instanceof CronTrigger)
|
|
|
- {
|
|
|
+ job.setTriggerState(triggerState.name());
|
|
|
+ if (trigger instanceof CronTrigger) {
|
|
|
CronTrigger cronTrigger = (CronTrigger) trigger;
|
|
|
String cronExpression = cronTrigger.getCronExpression();
|
|
|
job.setCronExpression(cronExpression);
|
|
@@ -139,9 +122,7 @@ public class QuartzManager
|
|
|
}
|
|
|
}
|
|
|
return jobList;
|
|
|
- }
|
|
|
- catch (SchedulerException e)
|
|
|
- {
|
|
|
+ } catch (SchedulerException e) {
|
|
|
TASK_LOG.error("Fail to get all jobs.", e);
|
|
|
throw new ExamCloudRuntimeException(e);
|
|
|
}
|
|
@@ -153,26 +134,21 @@ public class QuartzManager
|
|
|
* @author WANGWEI
|
|
|
* @return
|
|
|
*/
|
|
|
- public List<ScheduleJob> getRunningJobs()
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
+ public List<ScheduleJob> getRunningJobs() {
|
|
|
+ try {
|
|
|
Scheduler scheduler = schedulerFactoryBean.getScheduler();
|
|
|
List<JobExecutionContext> executingJobs = scheduler.getCurrentlyExecutingJobs();
|
|
|
List<ScheduleJob> jobList = new ArrayList<ScheduleJob>(executingJobs.size());
|
|
|
- for (JobExecutionContext executingJob : executingJobs)
|
|
|
- {
|
|
|
+ for (JobExecutionContext context : executingJobs) {
|
|
|
ScheduleJob job = new ScheduleJob();
|
|
|
- JobDetail jobDetail = executingJob.getJobDetail();
|
|
|
+ JobDetail jobDetail = context.getJobDetail();
|
|
|
JobKey jobKey = jobDetail.getKey();
|
|
|
- Trigger trigger = executingJob.getTrigger();
|
|
|
+ Trigger trigger = context.getTrigger();
|
|
|
job.setJobName(jobKey.getName());
|
|
|
job.setJobGroup(jobKey.getGroup());
|
|
|
- job.setDescription("触发器:" + trigger.getKey());
|
|
|
Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
|
|
|
- job.setJobStatus(triggerState.name());
|
|
|
- if (trigger instanceof CronTrigger)
|
|
|
- {
|
|
|
+ job.setTriggerState(triggerState.name());
|
|
|
+ if (trigger instanceof CronTrigger) {
|
|
|
CronTrigger cronTrigger = (CronTrigger) trigger;
|
|
|
String cronExpression = cronTrigger.getCronExpression();
|
|
|
job.setCronExpression(cronExpression);
|
|
@@ -180,9 +156,7 @@ public class QuartzManager
|
|
|
jobList.add(job);
|
|
|
}
|
|
|
return jobList;
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
+ } catch (Exception e) {
|
|
|
TASK_LOG.error("Fail to get running jobs.", e);
|
|
|
throw new ExamCloudRuntimeException(e);
|
|
|
}
|
|
@@ -194,16 +168,12 @@ public class QuartzManager
|
|
|
* @author WANGWEI
|
|
|
* @param scheduleJob
|
|
|
*/
|
|
|
- public void pauseJob(ScheduleJob scheduleJob)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
+ public void pauseJob(ScheduleJob scheduleJob) {
|
|
|
+ try {
|
|
|
Scheduler scheduler = schedulerFactoryBean.getScheduler();
|
|
|
JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
|
|
|
scheduler.pauseJob(jobKey);
|
|
|
- }
|
|
|
- catch (SchedulerException e)
|
|
|
- {
|
|
|
+ } catch (SchedulerException e) {
|
|
|
TASK_LOG.error("Fail to pause Job. job detail: " + JsonUtil.toJson(scheduleJob), e);
|
|
|
throw new ExamCloudRuntimeException(e);
|
|
|
}
|
|
@@ -215,16 +185,12 @@ public class QuartzManager
|
|
|
* @author WANGWEI
|
|
|
* @param scheduleJob
|
|
|
*/
|
|
|
- public void resumeJob(ScheduleJob scheduleJob)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
+ public void resumeJob(ScheduleJob scheduleJob) {
|
|
|
+ try {
|
|
|
Scheduler scheduler = schedulerFactoryBean.getScheduler();
|
|
|
JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
|
|
|
scheduler.resumeJob(jobKey);
|
|
|
- }
|
|
|
- catch (SchedulerException e)
|
|
|
- {
|
|
|
+ } catch (SchedulerException e) {
|
|
|
TASK_LOG.error("Fail to resume job. job detail: " + JsonUtil.toJson(scheduleJob), e);
|
|
|
throw new ExamCloudRuntimeException(e);
|
|
|
}
|
|
@@ -236,16 +202,12 @@ public class QuartzManager
|
|
|
* @author WANGWEI
|
|
|
* @param scheduleJob
|
|
|
*/
|
|
|
- public void deleteJob(ScheduleJob scheduleJob)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
+ public void deleteJob(ScheduleJob scheduleJob) {
|
|
|
+ try {
|
|
|
Scheduler scheduler = schedulerFactoryBean.getScheduler();
|
|
|
JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
|
|
|
scheduler.deleteJob(jobKey);
|
|
|
- }
|
|
|
- catch (SchedulerException e)
|
|
|
- {
|
|
|
+ } catch (SchedulerException e) {
|
|
|
TASK_LOG.error("Fail to delete job. job detail: " + JsonUtil.toJson(scheduleJob), e);
|
|
|
throw new ExamCloudRuntimeException(e);
|
|
|
}
|
|
@@ -258,16 +220,12 @@ public class QuartzManager
|
|
|
* @author WANGWEI
|
|
|
* @param scheduleJob
|
|
|
*/
|
|
|
- public void runJob(ScheduleJob scheduleJob)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
+ public void runJob(ScheduleJob scheduleJob) {
|
|
|
+ try {
|
|
|
Scheduler scheduler = schedulerFactoryBean.getScheduler();
|
|
|
JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
|
|
|
scheduler.triggerJob(jobKey);
|
|
|
- }
|
|
|
- catch (SchedulerException e)
|
|
|
- {
|
|
|
+ } catch (SchedulerException e) {
|
|
|
TASK_LOG.error("Fail to run job. job detail: " + JsonUtil.toJson(scheduleJob), e);
|
|
|
throw new ExamCloudRuntimeException(e);
|
|
|
}
|
|
@@ -279,10 +237,8 @@ public class QuartzManager
|
|
|
* @author WANGWEI
|
|
|
* @param scheduleJob
|
|
|
*/
|
|
|
- public void updateJobCronExpression(ScheduleJob scheduleJob)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
+ public void updateJobCronExpression(ScheduleJob scheduleJob) {
|
|
|
+ try {
|
|
|
Scheduler scheduler = schedulerFactoryBean.getScheduler();
|
|
|
|
|
|
TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName(),
|
|
@@ -297,9 +253,7 @@ public class QuartzManager
|
|
|
.withSchedule(scheduleBuilder).build();
|
|
|
|
|
|
scheduler.rescheduleJob(triggerKey, trigger);
|
|
|
- }
|
|
|
- catch (SchedulerException e)
|
|
|
- {
|
|
|
+ } catch (SchedulerException e) {
|
|
|
TASK_LOG.error("Fail to update job cron expression. job detail :"
|
|
|
+ JsonUtil.toJson(scheduleJob), e);
|
|
|
throw new ExamCloudRuntimeException(e);
|