Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add multiple triggers to single quartz job

I want to dynamically add triggers to a job, but can't find any helpful methods off of Scheduler

I though i would just be able to call the scheduleJob method an repetitively, but this gives me tthe ObjectAlreadyExists Exception "because one already exists with this identification".

How can i do this?

EDIT

private boolean scheduleLoadJob( XfuScheduleTimeInfo time )
{
    LoadScheduleJob job = new LoadScheduleJob( time );
    JobDetail detail;

    Integer id = Integer.valueOf( time.getScheduleId() );
    if( _hashMap.containsKey( id ) )
    {
        detail = _hashMap.get( Integer.valueOf( time.getScheduleId() ) );
    }
    else
    {
        detail = job.getDetail();
        _hashMap.put( id, detail );
    }

    try
    {
        Trigger newTrigger = job.getTrigger();
        _log.debug( "------" + newTrigger.getKey() );
        _quartzScheduler.scheduleJob( detail, newTrigger );
        return true;
    }
    catch( ParseException e )
    {
        _log.error( "Unable to parse cron expression for " + job.getInfo() );
        return false;
    }
    catch( SchedulerException e )
    {
        _log.error( "Job scheduling failed for " + job.getInfo() );
        return false;
    }
}

With Console Output

------ LoadJobs.Trigger-44

batch acquisition of 1 triggers

Producing instance of Job 'LoadJobs.Job-42', class=com.scheduling.LoadScheduleJob

Calling execute on job LoadJobs.Job-42

batch acquisition of 1 triggers

Job called for: 42 : 44

------ LoadJobs.Trigger-45

Job scheduling failed for 42 : 45 - 1/5 * * ? * *

like image 276
rediVider Avatar asked Jun 06 '11 21:06

rediVider


People also ask

How do you trigger multiple jobs in quartz scheduler?

If you want to schedule multiple jobs in your console application you can simply call Scheduler. ScheduleJob (IScheduler) passing the job and the trigger you've previously created: IJobDetail firstJob = JobBuilder.

How do you trigger Quartz scheduler?

All the Jobs registered in the Quartz Scheduler are uniquely identified by the JobKey which is composed of a name and group . You can fire the job which has a given JobKey immediately by calling triggerJob(JobKey jobKey) of your Scheduler instance. // Create a new Job JobKey jobKey = JobKey.

What are quartz triggers?

Identities. Jobs and Triggers are given identifying keys as they are registered with the Quartz scheduler. The keys of Jobs and Triggers (JobKey and TriggerKey) allow them to be placed into 'groups' which can be useful for organizing your jobs and triggers into categories such as “reporting jobs” and “maintenance jobs” ...


1 Answers

    CronTrigger trigger=null;
    CronTrigger trigger1=null;
    CronTrigger trigger2=null;

    JobDetail job = new JobDetail();
    job.setName("dummyJobName");
    job.setJobClass(ExampleJob.class);

    trigger = new CronTrigger();
    trigger.setName("AppTrigger");
    trigger.setGroup(job.getGroup());
    trigger.setJobName(job.getName());
    trigger.setJobGroup(job.getGroup());
    trigger.setCronExpression("*/2 * * * * ?");

    trigger1 = new CronTrigger();
    trigger1.setName("AppTrigger1");
    trigger1.setGroup(job.getGroup());
    trigger1.setJobName(job.getName());
    trigger1.setJobGroup(job.getGroup());
    trigger1.setCronExpression("*/2 * * * * ?");

    trigger2 = new CronTrigger();
    trigger2.setName("AppTrigger2");
    trigger2.setGroup(job.getGroup());
    trigger2.setJobName(job.getName());
    trigger2.setJobGroup(job.getGroup());
    trigger2.setCronExpression("*/2 * * * * ?");

    Scheduler scheduler = new StdSchedulerFactory().getScheduler();

    scheduler.start();
    scheduler.addJob(job, true);
    scheduler.scheduleJob(trigger);
    scheduler.scheduleJob(trigger1);
    scheduler.scheduleJob(trigger2);
like image 168
Anil Kumar Avatar answered Oct 13 '22 23:10

Anil Kumar