'Is there are way for CronTriggerImpl to get how many times it have been triggered
we can use SimpleTrigger getTimesTriggered to know how many times this schedule have been triggered, for CronTriggerImpl, is there a similar way to achieve this?
Solution 1:[1]
for CronTriggerImpl, is there a similar way to achieve this?
TLDR no.
But you can implement JobListener and register it with your scheduler like this:
scheduler.getListenerManager().addJobListener(myJobListener);
That way, you will be notified everytime job started and finished. You can retrieve Trigger instance from JobExecutionContext
Or similarly you can do with TriggerListener
scheduler.getListenerManager().addTriggerListener(myTriggerListener);
Solution 2:[2]
Here is an example program based on @rkosegi suggestion.
Using org.quartz.TriggerListener or org.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());
}
}
Using 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());
}
.....................
}
or Using 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());
}
................
}
Verify
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();
}
}
}
Output:
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | rkosegi |
| Solution 2 |
