是否有方法为CronTriggerImpl得到多少次它已被触发



我们可以使用SimpleTrigger gettimetriggered来知道这个调度被触发了多少次,对于CronTriggerImpl,是否有类似的方法来实现这一点?

对于CronTriggerImpl,是否有类似的方法来实现这一点?

TLDR。

但是你可以实现JobListener并像这样在你的调度程序中注册它:

scheduler.getListenerManager().addJobListener(myJobListener);

这样,每次作业开始和完成时都会通知您。您可以从jobeexecutioncontext

中检索触发器实例或者类似地,你可以使用TriggerListener

scheduler.getListenerManager().addTriggerListener(myTriggerListener);

下面是一个基于@rkosegi建议的示例程序。
使用org.quartz.TriggerListenerorg.quartz.JobListener

ByeJob.java

public class ByeJob implements Job {
private ByeService bs = new ByeService();
public void execute(JobExecutionContext context) throws JobExecutionException {
bs.sayGoodbye();
}
}

ByeService.java

public class ByeService {

public void sayGoodbye() {
System.out.println("Testing " + new Date());
}
}
使用<<p> strong> GlobalJobListner.java
public class GlobalJobListner implements JobListener {
private static final String TRIGGER_LISTENER_NAME = "GlobalJobListner";
AtomicInteger count = new AtomicInteger(0);
@Override
public String getName() {
return TRIGGER_LISTENER_NAME;
}
@Override
public void jobWasExecuted(JobExecutionContext context) {
String triggerName = context.getTrigger().getKey().toString();
String jobName = context.getJobDetail().getKey().toString();
count.incrementAndGet();
System.out.println("trigger : " + triggerName + " is fired and " + "total count=" + count.get());
System.out.println("job : " + jobName + " is fired and " + "total count=" + count.get());
}
.....................
}

或使用GlobalTriggerListener.java

public class GlobalTriggerListener implements TriggerListener {

private static final String TRIGGER_LISTENER_NAME = "GlobalTriggerListener";
AtomicInteger count = new AtomicInteger(0);

@Override
public String getName() {
return TRIGGER_LISTENER_NAME;
}

@Override
public void triggerFired(Trigger trigger, JobExecutionContext context) {
String triggerName = context.getTrigger().getKey().toString();
String jobName = context.getJobDetail().getKey().toString();
count.incrementAndGet();
System.out.println("trigger : " + triggerName + " is fired and " + "total count=" + count.get());
System.out.println("job : " + jobName + " is fired and " + "total count=" + count.get());
}
................
}

验证

public class Tester {
public static void main(String[] args) {
try {
JobDetail job = JobBuilder.newJob(ByeJob.class).withIdentity("byeJob", "group1").build();
CronTriggerImpl trigger = new CronTriggerImpl();
trigger.setName("T1");
trigger.setCronExpression("0/5 * * * * ?");
Scheduler scheduler2 = new StdSchedulerFactory().getScheduler();
// register global trigger
scheduler2.getListenerManager().addTriggerListener(new GlobalTriggerListener());
// register global listner
scheduler2.getListenerManager().addJobListener(new GlobalJobListner());
scheduler2.start();
scheduler2.scheduleJob(job, trigger);
} catch (Exception e) {
e.printStackTrace();
}
}
}

输出:

13:46:43.402 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 0 triggers
13:46:43.408 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
13:46:45.004 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.simpl.PropertySettingJobFactory - Producing instance of Job 'group1.byeJob', class=com.example.demo.ByeJob
trigger : DEFAULT.T1 is fired and total count=1
job : group1.byeJob is fired and total count=1
trigger : DEFAULT.T1 is fired and total count=1
job : group1.byeJob is fired and total count=1
13:46:45.009 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
13:46:45.009 [DefaultQuartzScheduler_Worker-1] DEBUG org.quartz.core.JobRunShell - Calling execute on job group1.byeJob
Testing Fri Apr 22 13:46:45 IST 2022
13:46:50.002 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.simpl.PropertySettingJobFactory - Producing instance of Job 'group1.byeJob', class=com.example.demo.ByeJob
13:46:50.003 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
trigger : DEFAULT.T1 is fired and total count=2
job : group1.byeJob is fired and total count=2
trigger : DEFAULT.T1 is fired and total count=2
job : group1.byeJob is fired and total count=2
13:46:50.003 [DefaultQuartzScheduler_Worker-2] DEBUG org.quartz.core.JobRunShell - Calling execute on job group1.byeJob
Testing Fri Apr 22 13:46:50 IST 2022
13:46:55.003 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.simpl.PropertySettingJobFactory - Producing instance of Job 'group1.byeJob', class=com.example.demo.ByeJob
trigger : DEFAULT.T1 is fired and total count=3
job : group1.byeJob is fired and total count=3
trigger : DEFAULT.T1 is fired and total count=3
job : group1.byeJob is fired and total count=3

相关内容

最新更新