Recently I was in a scenario where I need to update job data map of the quartz job trigger at runtime, After a little bit research and exploring the API for quartz 2.2.1, I found the required solution.
Firstly I have created the cron trigger, where I have set the identity of the cron trigger as:
I have created the identity with randomUUID()
1 |
triggerName = UUID.randomUUID() |
And then set that triggerName as identity of cron trigger:
1 2 3 4 5 6 7 8 9 |
JobDataMap jobDataMap = new JobDataMap([username: "Abdullah", password: "password"]) cronExpression = "your cron expression" CronTrigger trigger = TriggerBuilder.newTrigger() .withIdentity(triggerName) .forJob(MyJob.class.name.toString()) .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).usingJobData(jobDataMap) .build() MyJob.schedule(trigger) |
And I updated that trigger at runtime by fetching the required trigger with the help of triggerName (which I have stored in the database at the time of creation of cron trigger) from the bundle of triggers as:
1 2 3 4 5 6 7 8 9 10 11 |
TriggerKey triggerKey = new TriggerKey(triggerName); try { Trigger trigger = quartzScheduler.getTrigger(triggerKey) if (trigger?.key?.name) { trigger.jobDataMap['username'] = "Abdullah" trigger.jobDataMap['password'] = "new-password" } quartzScheduler.rescheduleJob(triggerKey, trigger) } catch (SchedulerException ex) { log.error("Scheduler Exception: ${ex.printStackTrace()}") } |
Recent Comments