我有石英调度程序,我已经为指定的时间(每天)创建了一个触发器。而且我还为另一个指定的时间创建了另一个触发器(每三个月一次)
以下是我将两个触发器安排到单个调度程序的代码。
Scheduler sch = new StdSchedulerFactory().getScheduler();
JobDetail jobDetail = JobBuilder.newJob(MFRScheduler.class)
.withIdentity("firstJob", "group1").build();
JobDetail jobDetail1 = JobBuilder.newJob(MFRScheduler.class)
.withIdentity("cleanTrigger", "group2").build();
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("firstJob", "group1")
.withSchedule(
CronScheduleBuilder.cronSchedule(schduledTime))
.build();
Trigger houseKeepingTrigger = TriggerBuilder
.newTrigger()
.withIdentity("mfrJobHouseKeeping","group2")
.withSchedule(
CronScheduleBuilder.cronSchedule(cleanTrigerTime)
)
.build();
sch.start();
sch.scheduleJob(jobDetail, trigger);
sch.scheduleJob(jobDetail1, cleanTrigger);
我覆盖了execute()
org.quartz.Job
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
// TODO Auto-generated method stub
_log.info("--- This is in Execute method");
try{
Thread thread = new Thread(new FIRSTProcess("FIRST_THREAD"),"thread2");
thread.start();
thread.setName("FIRST_THREAD");
thread = null;
Thread.sleep(3000);
}catch(Exception e)
{
e.printStackTrace();
}
我的问题在这里一个触发器每天触发,execute()
方法中的内容将执行,因为我只有一个方法execute()
将被执行两个触发器。
我需要的是execute
方法中的内容只有当我的firstTrigger
触发我的第二个触发器触发时才会执行我想做其他事情而不是重复{{1 }}
请解释一下如何调整两个触发器的工作原理。
提前致谢
答案 0 :(得分:0)
由于你想做不同的工作,我建议创建不同的工作实现(例如MFRSchedulerHouseKeeping.class)。然后,您可以将所需的触发器分配给特定的作业。
Scheduler sch = new StdSchedulerFactory().getScheduler();
JobDetail jobDetail = JobBuilder.newJob(MFRScheduler.class)
.withIdentity("firstJob", "group1").build();
JobDetail jobDetail1 = JobBuilder.newJob(MFRSchedulerHouseKeeping.class)
.withIdentity("houseKeepingJob", "group2").build();
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("firstJob", "group1")
.withSchedule(
CronScheduleBuilder.cronSchedule(schduledTime))
.build();
Trigger houseKeepingTrigger = TriggerBuilder
.newTrigger()
.withIdentity("mfrJobHouseKeeping","group2")
.withSchedule(
CronScheduleBuilder.cronSchedule(cleanTrigerTime)
)
.build();
sch.start();
sch.scheduleJob(jobDetail, trigger);
sch.scheduleJob(jobDetail1, cleanTrigger);
另一种方法是通过JobExecutionContext
从getTrigger()
查询触发器,并代表结果执行相应的工作。
@Override
public void execute(JobExecutionContext context ) throws JobExecutionException {
final String triggerName = context.getTrigger().getKey().getName();
if (TRIGGER_NAME_1.equals(triggerName)) {
// do work ...
}
// other cases...
}