Wednesday 29 October 2014

JobExecutionContext interface

JobExecutionContext provides the job instance with information about its run-time environment, including a handle to the scheduler that executed it, a handle to the trigger that triggered the execution, the job's JobDetail object, and a few other items.
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Trigger;

public class HelloWorld implements Job{
    @Override
    public void execute(JobExecutionContext jec) throws JobExecutionException {

        Date fireTime = jec.getFireTime();

        /* Get Job Details */
        JobDetail jobDetails = jec.getJobDetail();
        String description = jobDetails.getDescription();
        
        Class jobClass = jobDetails.getClass();
        String className = jobClass.getName();

        /* Get the job instance */
        Job myJob = jec.getJobInstance();
        String myJobName = myJob.getClass().getName();

        long jobRunTime = jec.getJobRunTime();

        Date prevFireTime = jec.getPreviousFireTime();
        Date nextFireTime = jec.getNextFireTime();
        Date scheduledFireTIme = jec.getScheduledFireTime();

        /* Get the trigger details */
        Trigger trigger = jec.getTrigger();
        String triggerDescription = trigger.getDescription();

        System.out.println("***************************************");
        System.out.println("Fire Time is " + fireTime);
        System.out.println("description is " + description);
        System.out.println("className is " + className);
        System.out.println("myJobName is " + myJobName);
        System.out.println("jobRunTime is " + jobRunTime);
        System.out.println("prevFireTime is " + prevFireTime);
        System.out.println("nextFireTime is " + nextFireTime);
        System.out.println("scheduledFireTIme is " + scheduledFireTIme);
        System.out.println("triggerDescription is " + triggerDescription);
        System.out.println("***************************************\n\n");
    }
}

import java.util.Date;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
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(HelloWorld.class);
        jobDetail.setDescription("Simple Hello World Application");

        //Creating schedule time with trigger
  SimpleTriggerImpl simpleTrigger = new SimpleTriggerImpl();
  simpleTrigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
  simpleTrigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
  simpleTrigger.setRepeatInterval(3000);
  simpleTrigger.setName("FirstTrigger");
        simpleTrigger.setDescription("Simple Trigger");

  //Start scheduler
  scheduler.start();
  scheduler.scheduleJob(jobDetail,simpleTrigger);

    }
}


Sample 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.
***************************************
Fire Time is Tue Oct 28 17:03:50 IST 2014
description is Simple Hello World Application
className is org.quartz.impl.JobDetailImpl
myJobName is HelloWorld
jobRunTime is -1
prevFireTime is null
nextFireTime is Tue Oct 28 17:03:53 IST 2014
scheduledFireTIme is Tue Oct 28 17:03:50 IST 2014
triggerDescription is Simple Trigger
***************************************


***************************************
Fire Time is Tue Oct 28 17:03:53 IST 2014
description is Simple Hello World Application
className is org.quartz.impl.JobDetailImpl
myJobName is HelloWorld
jobRunTime is -1
prevFireTime is Tue Oct 28 17:03:50 IST 2014
nextFireTime is Tue Oct 28 17:03:56 IST 2014
scheduledFireTIme is Tue Oct 28 17:03:53 IST 2014
triggerDescription is Simple Trigger
***************************************

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment