4

I am using quartz framework to add and schedule jobs and triggers. The requirement is to add triggers dynamically for the already existing job in the database. While I am trying to achieve this with below code, I am getting org.quartz.ObjectAlreadyExistsException: Unable to store Job : 'EBP1.AAA', because one already exists with this identification. Job AAA with JobGroup EBP1 is already into the database. I just want to add new trigger for the same job. Kindly help how to achieve this.

String jobName = "AAA";
jobTrigger = jobName + "Trigger"+ (int )(Math.random() * 50 + 1);
String jobGroup = "EBP1";

JobDetail jobDetail = JobBuilder.newJob(ScheduleJob.class).withIdentity(jobTrigger, jobGroup).storeDurably()
                    .build();
            if (scheduler.checkExists(JobKey.jobKey(jobName, jobGroup))) {
                System.out.println("Job exist");
            } else {
                System.out.println("New job");
                scheduler.addJob(jobDetail, true);
            }

            CronTrigger trigger1 = (CronTrigger)(newTrigger().withIdentity(jobTrigger, jobGroup).withDescription("default description").forJob(jobDetail)
                    .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression)).startAt(futureDate(500, IntervalUnit.MILLISECOND)).build());

            scheduler.scheduleJob(jobDetail, trigger1);
Tanu Garg
  • 3,007
  • 4
  • 21
  • 29

3 Answers3

1

Quick question: is the sample code exactly the same as the one you get exception from?
Or maybe the sample code is not complete and you need to update it.

Anyhow, I think there is a bug in your current sample code:
- you create "jobDetail" with trigger's identity, not the "jobName" - withIdentity(jobTrigger, jobGroup)

To add new trigger to existing job, do something like this:

JobKey myJobKey = new JobKey(jobName, jobGroup);

CronTrigger trigger1 = (CronTrigger)(newTrigger().withIdentity(jobTrigger, jobGroup)
    .withDescription("default description").forJob(myJobKey)
    .withSchedule(CronScheduleBuilder.cronSchedule(cronExpression))
    .startAt(futureDate(500, IntervalUnit.MILLISECOND))
    .build());

scheduler.scheduleJob(trigger1);

The JavaDoc for 2-parameter method scheduler.scheduleJob(JobDetail, Trigger) clearly says,
that it will try to CREATE the job based on provided JobDetail
- so it makes sense why you get ObjectAlreadyExistsException

ljader
  • 620
  • 2
  • 8
  • 22
0

can you replace

            scheduler.scheduleJob(jobDetail, trigger1);

with the below and try again

            scheduler.scheduleJob(trigger1);
Srinivas
  • 400
  • 1
  • 8
  • I tried but this solution did not work. scheduler.scheduleJob(jobDetail, triggerList, true); worked where triggerList is Set . – Tanu Garg Apr 11 '19 at 18:54
0
  1. build trigger and associate with job by for job method
  2. schedule job with this trigger
CronTrigger cronTrigger = TriggerBuilder.newTrigger()
.withIdentity(trigger.getName(),trigger.getGroup())
.withSchedule(cronScheduleBuilder)
.forJob(trigger.getJobName(),trigger.getJobGroup())
.build();
     
scheduler.scheduleJob(cronTrigger);
Stephan Hogenboom
  • 1,543
  • 2
  • 17
  • 29