You can create Scheduler listener by implementing
SchedulerListener interface.
import org.quartz.JobDetail; import org.quartz.JobKey; import org.quartz.SchedulerException; import org.quartz.SchedulerListener; import org.quartz.Trigger; import org.quartz.TriggerKey; public class MySchedulerListener implements SchedulerListener{ public void jobScheduled(Trigger trgr) { System.out.println("Job Scheduled"); } public void jobUnscheduled(TriggerKey tk) { System.out.println("Job Unscheduled"); } public void triggerFinalized(Trigger trgr) { System.out.println("Trigger finalized"); } public void triggerPaused(TriggerKey tk) { System.out.println("Trigger paused"); } public void triggersPaused(String string) { System.out.println("Triggers paused"); } public void triggerResumed(TriggerKey tk) { System.out.println("Trigger Resumed"); } public void triggersResumed(String string) { System.out.println("Triggers Resumed"); } public void jobAdded(JobDetail jd) { System.out.println("Job Added"); } public void jobDeleted(JobKey jobkey) { System.out.println("Job Deleted"); } public void jobPaused(JobKey jobkey) { System.out.println("Job Paused"); } public void jobsPaused(String string) { System.out.println("Jobs paused"); } public void jobResumed(JobKey jobkey) { System.out.println("Job Resumed"); } public void jobsResumed(String string) { System.out.println("Jobs Resumed"); } public void schedulerError(String string, SchedulerException se) { System.out.println("Error occured in Scheduler"); } public void schedulerInStandbyMode() { System.out.println("Scheduler moved to standby mode"); } public void schedulerStarted() { System.out.println("Scheduler started"); } public void schedulerStarting() { System.out.println("Scheduler starting"); } public void schedulerShutdown() { System.out.println("Scheduler shutdowned"); } public void schedulerShuttingdown() { System.out.println("Scheduler is shutting down"); } public void schedulingDataCleared() { System.out.println("Scheduling data cleared"); } }
Step
2: Add listener to the scheduler.
Ex:
scheduler.getListenerManager().addSchedulerListener(new
MySchedulerListener());
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().addSchedulerListener(new MySchedulerListener()); //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. Scheduler starting Scheduler started Job Added Job Scheduled Job in execution Job in execution Job in execution Job in execution Trigger finalized
No comments:
Post a Comment