Thursday, 30 October 2014

Create your job listener


You can create Job listener by implementing JobListener interface.

Step 1: Create Job listener by implementing JobListenerinterface.

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;

public class MyJobListener implements JobListener{

    public String getName() {
        return "MyJobListener";
    }

    public void jobToBeExecuted(JobExecutionContext jec) {
        System.out.println("Job is about to be executed");
    }

    public void jobExecutionVetoed(JobExecutionContext jec) {
        System.out.println("Job was about to be executed");
    }

    public void jobWasExecuted(JobExecutionContext jec, JobExecutionException jee) {
        System.out.println("Job finished execution");
    }
}


Step 2: Add job listener to the scheduler.
Ex: scheduler.getListenerManager().addJobListener(new MyJobListener());

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().addJobListener(new MyJobListener());

  //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.
Job is about to be executed
Job in execution
Job finished execution
Job is about to be executed
Job in execution
Job finished execution
Job is about to be executed
Job in execution
Job finished execution
Job is about to be executed
Job in execution
Job finished execution

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment