Wednesday, 29 October 2014

JobDetail interface


JobDetail object is created by the your program at the time the job is added to the scheduler.

import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

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

        /* Get Job Details */
        JobDetail jobDetails = jec.getJobDetail();
        String description = jobDetails.getDescription();
        Class jobClass = jobDetails.getClass();
        String className = jobClass.getName();
        boolean isDurable = jobDetails.isDurable();
        boolean isConcExecDisAlowed = jobDetails.isConcurrentExectionDisallowed();
        boolean isPersistant = jobDetails.isPersistJobDataAfterExecution();
        boolean isReExecAfterFailed = jobDetails.requestsRecovery();

        System.out.println("***************************************");
        System.out.println("Details about job");
        System.out.println("description is " + description);
        System.out.println("className is " + className);
        System.out.println("Is job Durable " + isDurable);
        System.out.println("Is Concurrent Execution dis allowed " + isConcExecDisAlowed);
        System.out.println("Is Job persistant " + isPersistant);
        System.out.print("Is Job re executed after 'recovery' or 'fail-over' situation is encountered ");
        System.out.println(isReExecAfterFailed);
        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");
        jobDetail.setRequestsRecovery(true);
        jobDetail.setDurability(true);

        //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.
***************************************
Details about job
description is Simple Hello World Application
className is org.quartz.impl.JobDetailImpl
Is job Durable true
Is Concurrent Execution dis allowed false
Is Job persistant false
Is Job re executed after 'recovery' or 'fail-over' situation is encountered true
***************************************


***************************************
Details about job
description is Simple Hello World Application
className is org.quartz.impl.JobDetailImpl
Is job Durable true
Is Concurrent Execution dis allowed false
Is Job persistant false
Is Job re executed after 'recovery' or 'fail-over' situation is encountered true
***************************************

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment