向单个石英作业添加多个触发器

时间:2011-06-06 21:25:03

标签: java dynamic quartz-scheduler repeat

我想动态地向作业添加触发器,但无法从Scheduler

中找到任何有用的方法

我虽然我只能重复调用scheduleJob方法,但是这给了我ObjectAlreadyExists Exception“因为已经存在这个标识”。

我该怎么做?

修改

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;
    }
}

使用控制台输出

------ LoadJobs.Trigger-44

批量获取1个触发器

生成Job'LoadJobs.Job-42'的实例,class = com.scheduling.LoadScheduleJob

调用执行LoadJobs.Job-42

批量获取1个触发器

求职:42:44

------ LoadJobs.Trigger-45

42:45 - 1/5 * *的作业调度失败? * *

3 个答案:

答案 0 :(得分:10)

This post给出了提示,但结论(schedulerInstance.add(trigger))自Quartz 2.01起无效。

在将作业分配给触发器之后使用以下内容(一种方法是使用TriggerBuilder的forJob方法)

 schedulerInstance.scheduleJob( newTrigger )

答案 1 :(得分:5)

    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);

答案 2 :(得分:1)

您可以重复调用scheduleJob。只需确保为每个Trigger指定一个唯一的名称/组。

请参阅TriggerBuilder.withIdentity:http://www.quartz-scheduler.org/docs/api/2.0.0/index.html