更新cron作业的触发器

时间:2016-08-12 16:04:11

标签: grails quartz-scheduler crontrigger

我有一个cron作业,它有多个触发器,所有触发器都被调度为5分钟的间隔,现在我需要在运行时更新作业数据图,因为我需要更新但需要更新的特定触发器我没有得到那个特定的触发器,我正在做类似的事情

String cronExpression = "0 0/5 * * * ?"
String triggername = "mytrigger" + System.currentTimeMillis()
JobDataMap jobDataMap = new JobDataMap([host: config.host, port: config.port, username: config.username, password: config.password])
CronTrigger trigger = TriggerBuilder.newTrigger()
        .withIdentity(triggername)
        .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).usingJobData(jobDataMap)
        .build()
MyJob.schedule(trigger)

任何想法如何才能获得我必须更新的特定触发器?

2 个答案:

答案 0 :(得分:0)

尝试类似的东西:

List<Trigger> triggers = quartzScheduler.jobGroupNames.collect {
    quartzScheduler.getJobKeys(GroupMatcher.groupEquals(it)).collect {
        quartzScheduler.getTriggersOfJob(it)
    }
}.flatten()

它将返回所有预定的触发器。获得它们后,您可以在JobDataMap中执行所需的操作。

我建议使用熟悉的触发器标识,在运行时更容易找到特定的触发器标识。

答案 1 :(得分:0)

我将触发器名称保存在数据库中,当我需要更新该触发器时,我只需像这样获取该触发器:

TriggerKey triggerKey = new TriggerKey(triggerName_from_db);
try {
    Trigger trigger = quartzScheduler.getTrigger(triggerKey)
    if (trigger?.key?.name) {
        trigger.jobDataMap['host'] = config.host
        trigger.jobDataMap['port'] = config.port
        trigger.jobDataMap['username'] = config.username
        trigger.jobDataMap['password'] = config.password
    }
    quartzScheduler.rescheduleJob(triggerKey, trigger)
    }catch (SchedulerException ex) {
    log.error ex.toString()
}