添加定时任务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的主要内容,如果未能解决你的问题,请参考以下文章

Oracle添加定时任务

jenkins 批量添加任务job

#yyds干货盘点# springboot整合Elastic Job实现分片配置定时任务

quartz定时任务_job实现类中获取传参02

定时任务管理工具 xxl_job 的使用

Oracle 定时任务job实际应用