You can
create Trigger listener by implementing TriggerListener interface.
Step 1: Create TriggerListener by
implementing TriggerListener interface.
import org.quartz.JobExecutionContext; import org.quartz.Trigger; import org.quartz.Trigger.CompletedExecutionInstruction; import org.quartz.TriggerListener; public class MyTriggerListener implements TriggerListener{ public String getName() { return "MyTriggerListener"; } public void triggerFired(Trigger trgr, JobExecutionContext jec) { System.out.println("Trigger fired"); } public boolean vetoJobExecution(Trigger trgr, JobExecutionContext jec) { System.out.println("called after the triggerFired method of this interface. "); return true; } public void triggerMisfired(Trigger trgr) { System.out.println("Trigger misfired"); } public void triggerComplete(Trigger trgr, JobExecutionContext jec, CompletedExecutionInstruction cei) { System.out.println("Trigger completed"); } }
Step 2: Add trigger listener to the
scheduler.
Ex: scheduler.getListenerManager().addTriggerListener(new
MyTriggerListener());
Task.java
import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class Task implements Job{ @Override public void execute(JobExecutionContext jec) throws JobExecutionException { System.out.println("Job in execution"); } }
QuartzSchedulerEx.java
import java.util.Date; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.impl.JobDetailImpl; import org.quartz.impl.StdSchedulerFactory; import org.quartz.impl.triggers.SimpleTriggerImpl; public class QuartzSchedulerEx { public static void main(String args[]) throws SchedulerException{ // Initiate a Schedule Factory SchedulerFactory schedulerFactory = new StdSchedulerFactory(); // Retrieve a scheduler from schedule factory Scheduler scheduler = schedulerFactory.getScheduler(); //Creating JobDetailImpl and link to our Job class JobDetailImpl jobDetail = new JobDetailImpl(); jobDetail.setName("First Job"); jobDetail.setJobClass(Task.class); jobDetail.setDescription("Simple Task Application"); jobDetail.setRequestsRecovery(true); jobDetail.setDurability(true); //Creating schedule time with trigger SimpleTriggerImpl simpleTrigger = new SimpleTriggerImpl(); simpleTrigger.setStartTime(new Date(System.currentTimeMillis() + 1000)); simpleTrigger.setRepeatCount(3); simpleTrigger.setRepeatInterval(1000); simpleTrigger.setName("FirstTrigger"); simpleTrigger.setDescription("Simple Trigger"); /* Add a trigger listener to this scheduler */ scheduler.getListenerManager().addTriggerListener(new MyTriggerListener()); //Start scheduler scheduler.start(); scheduler.scheduleJob(jobDetail,simpleTrigger); } }
Output
log4j:WARN No appenders could be found for logger (org.quartz.impl.StdSchedulerFactory). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. Trigger fired called after the triggerFired method of this interface. Trigger fired called after the triggerFired method of this interface. Trigger fired called after the triggerFired method of this interface. Trigger fired called after the triggerFired method of this interface.
No comments:
Post a Comment