Thursday, 30 October 2014

Simple Trigger


Simple Trigger is used to fire a Job at a given moment in time, and optionally repeated at a specified interval.

Fields in SimpleTrigger interface
Field
Description
MISFIRE_INSTRUCTION_FIRE_NOW
Instructs the Scheduler that upon a mis-fire situation, the SimpleTrigger wants to be fired now by Scheduler.
MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_EXISTING_REPEAT_COUNT
Instructs the Scheduler that upon a mis-fire situation, the SimpleTrigger wants to be re-scheduled to 'now' (even if the associated Calendar excludes 'now') with the repeat count left as-is.
MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT
Instructs the Scheduler that upon a mis-fire situation, the SimpleTrigger wants to be re-scheduled to 'now' (even if the associated Calendar excludes 'now') with the repeat count set to what it would be, if it had not missed any firings.
MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_REMAINING_COUNT
Instructs the Scheduler that upon a mis-fire situation, the SimpleTrigger wants to be re-scheduled to the next scheduled time after 'now' - taking into account any associated Calendar, and with the repeat count set to what it would be, if it had not missed any firings.
MISFIRE_INSTRUCTION_RESCHEDULE_NEXT_WITH_EXISTING_COUNT
Instructs the Scheduler that upon a mis-fire situation, the SimpleTrigger wants to be re-scheduled to the next scheduled time after 'now' - taking into account any associated Calendar, and with the repeat count left unchanged.
REPEAT_INDEFINITELY
Used to indicate the 'repeat count' of the trigger is indefinite.

Task.java
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Trigger;

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

        /* Get Job Details */
        JobDetail jobDetails = jec.getJobDetail();

        /* Get JobDataMap */
        JobDataMap jobDataMap = jobDetails.getJobDataMap();
        String author = jobDataMap.getString("author");
        String topic= jobDataMap.getString("topic");
        String about = jobDataMap.getString("about");

        /* Get Trigger handle */
        Trigger trigger = jec.getTrigger();

        /* Get DataMap associated with Trigger */
        JobDataMap triggerDataMap =trigger.getJobDataMap();
        String repeatInterval = triggerDataMap.getString("repeatInterval");
        String triggerName = triggerDataMap.getString("triggerName");

        System.out.println("***************************************");
        System.out.println("Information in JobDataMap");
        System.out.println("author : " + author);
        System.out.println("topic : " + topic);
        System.out.println("about : " + about);
        System.out.println("Information in triggerDataMap");
        System.out.println("Repeat Interval is : " + repeatInterval);
        System.out.println("Trigger Name is : " + triggerName);
        System.out.println("***************************************\n\n");
    }
}


QuartzSchedulerEx.java
import java.util.Date;
import org.quartz.JobDataMap;
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(Task.class);
        jobDetail.setDescription("Simple Task Application");
        jobDetail.setRequestsRecovery(true);
        jobDetail.setDurability(true);

        /* Set JobDataMap for the Job */
        JobDataMap jobDataMap = new JobDataMap ();
        jobDataMap.put("author", "Krishna");
        jobDataMap.put("topic", "Quartz Scheduler");
        jobDataMap.put("about", "JobDataMap");
        jobDetail.setJobDataMap(jobDataMap);

        //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");

        /* Set triggerDataMap for the Job */
        JobDataMap triggerDataMap = new JobDataMap ();
        triggerDataMap.put("repeatInterval", "3000");
        triggerDataMap.put("triggerName", "firstTrigger");
        simpleTrigger.setJobDataMap(triggerDataMap);

  //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.
***************************************
Information in JobDataMap
author : Krishna
topic : Quartz Scheduler
about : JobDataMap
Information in triggerDataMap
Repeat Interval is : 3000
Trigger Name is : firstTrigger
***************************************


***************************************
Information in JobDataMap
author : Krishna
topic : Quartz Scheduler
about : JobDataMap
Information in triggerDataMap
Repeat Interval is : 3000
Trigger Name is : firstTrigger
***************************************

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment