Add multiple triggers to single quartz job

rediVider picture rediVider · Jun 6, 2011 · Viewed 26k times · Source

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 * * ? * *

Answer

Jake Roberts picture Jake Roberts · Jun 7, 2011

This post gives a hint, but the conclusion ( schedulerInstance.add(trigger) ) is not valid as of Quartz 2.01.

Instead use the following, after assinging the job to the trigger ( one way is using the TriggerBuilder's forJob method )

 schedulerInstance.scheduleJob( newTrigger )