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

2022-09-04 20:30:02

我想向作业动态添加触发器,但在计划程序中找不到任何有用的方法

我虽然只能重复地调用schuleJob方法,但这给了我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;
    }
}

带控制台输出

------ 负载作业触发器-44

批量采集 1 个触发器

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

调用在作业 LoadJobs.Job-42 上执行

批量采集 1 个触发器

职位要求: 42 : 44

------负载作业触发器-45

作业计划失败 42 : 45 - 1/5 * * ?* *


答案 1

这篇文章给出了一个提示,但结论( schedulerInstance.add(trigger) ) 在 Quartz 2.01 之前无效。

相反,在将作业评估到触发器后,请使用以下内容(一种方法是使用TriggerBuilder的forJob方法)

 schedulerInstance.scheduleJob( newTrigger )

答案 2
    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);

推荐