码迷,mamicode.com
首页 > 其他好文 > 详细

添加定时任务Job

时间:2020-04-16 16:48:47      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:except   shutdown   sch   etl   str   map   参数   with   info   

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

标签:except   shutdown   sch   etl   str   map   参数   with   info   

原文地址:https://blog.51cto.com/7218743/2487812

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!