添加定时任务Job
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了添加定时任务Job相关的知识,希望对你有一定的参考价值。
private static String JOB_NAME = "_JOB_NAME";private static String JOB_GROUP_NAME = "EXTJWEB_JOBGROUP_NAME";
private static String TRIGGER_NAME = "_TRIGGER" + "_NAME";
private static String TRIGGER_GROUP_NAME = "EXTJWEB_TRIGGERGROUP_NAME";
private static final Logger logger = LoggerFactory.getLogger(CycleRuleQuartzManager.class);
public static String addJob(Scheduler sc, String taskId, String cron) throws SchedulerException,
ClassNotFoundException {
logger.info("---------新增周期规则任务请求参数:taskId=" + taskId + ",cron=" + cron);
if (StringUtil.isEmpty(cron)) {
logger.info("addJob cron 为空");
return "0";
}
if (StringUtil.isEmpty(taskId)) {
logger.info("addJob taskId 为空");
return "0";
}
Map<String, Object> paramMap = new HashMap<String, Object>();
paramMap.put("taskId", taskId);
JobDataMap jobParam = new JobDataMap(paramMap);
JobDetail jobDetail =
JobBuilder.newJob(CycleRuleQuartzJob.class).withIdentity(taskId + JOB_NAME, JOB_GROUP_NAME)
.setJobData(jobParam).build();
TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
triggerBuilder.withIdentity(taskId + TRIGGER_NAME, TRIGGER_GROUP_NAME);
triggerBuilder.startNow();
triggerBuilder.withSchedule(CronScheduleBuilder.cronSchedule(cron));
CronTrigger trigger = (CronTrigger) triggerBuilder.build();
sc.scheduleJob(jobDetail, trigger);
if (!sc.isShutdown()) {
sc.start();
}
logger.info("---------新增周期规则任务成功---------");
return "1";
}
以上是关于添加定时任务Job的主要内容,如果未能解决你的问题,请参考以下文章