xxl-job如何停止某一个任务?

时间:2022-12-22 09:57:21

停止,通过quartz调度器的unscheduleJob()方法,取消quartz的调度。

public ReturnT<String> stop(int id) {
XxlJobInfo xxlJobInfo = xxlJobInfoDao.loadById(id);
String group = String.valueOf(xxlJobInfo.getJobGroup());
String name = String.valueOf(xxlJobInfo.getId());

try {
// bind quartz
boolean ret = XxlJobDynamicScheduler.removeJob(name, group);
return ret?ReturnT.SUCCESS:ReturnT.FAIL;
} catch (SchedulerException e) {
logger.error(e.getMessage(), e);
return ReturnT.FAIL;
}
}

public static boolean removeJob(String jobName, String jobGroup) throws SchedulerException {

TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroup);

if (scheduler.checkExists(triggerKey)) {
scheduler.unscheduleJob(triggerKey); // trigger + job
}

logger.info(">>>>>>>>>>> removeJob success, triggerKey:{}", triggerKey);
return true;
}