Monday, 29 July 2019

Quartz: Working with SimpleTrigger


SimpleTrigger is used to execute a job at a given moment of time and repeat the job execution at specific intervals.



As you see above image, SimpleTrigger is an interface that extends Trigger interface. SimpleTriggerImpl class implements SimpleTrigger interface.

SimpleTrigger interface provide APIs
a.   To get the time interval in milliseconds at which the SimpleTrigger should repeat.
b.   To get the number of times the SimpleTrigger has already fired.
c.    To get the number of times the SimpleTrigger should repeat, after which it will be automatically deleted.
d.   Get the TriggerBuilder instance

Find the below working example.

PrintJob.java
package com.sample.jobs;

import java.util.Map.Entry;

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

public class PrintJob implements Job {
 public void execute(JobExecutionContext jec) throws JobExecutionException {

  System.out.println("********************************************");
  System.out.println("Printing Job data map information");
  JobDataMap jobDataMap = jec.getJobDetail().getJobDataMap();
  for (Entry<String, Object> entry : jobDataMap.entrySet()) {
   System.out.println(entry.getKey() + " : " + entry.getValue());
  }

  System.out.println("\nPrinting Trigger Job data map information");
  JobDataMap triggerJobDataMap = jec.getTrigger().getJobDataMap();
  for (Entry<String, Object> entry : triggerJobDataMap.entrySet()) {
   System.out.println(entry.getKey() + " : " + entry.getValue());
  }
  System.out.println("********************************************");

 }
}


QuartzSchedulerEx.java
package com.sample.app;

import static org.quartz.DateBuilder.futureDate;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;
import static org.quartz.TriggerKey.triggerKey;

import org.quartz.DateBuilder.IntervalUnit;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;

import com.sample.jobs.PrintJob;

public class QuartzSchedulerEx {
 public static void main(String args[]) throws SchedulerException, InterruptedException {

  // Initiate a Schedule Factory
  SchedulerFactory schedulerFactory = new StdSchedulerFactory();

  // Retrieve a scheduler from schedule factory
  Scheduler scheduler = schedulerFactory.getScheduler();

  JobDataMap jobDataMap = new JobDataMap();
  jobDataMap.put("Description", "This job prints some message to console");

  JobDataMap triggerJobDataMap = new JobDataMap();
  triggerJobDataMap.put("userName", "krishna123");
  triggerJobDataMap.put("token", "myToken4321");

  // Creating JobDetailImpl and link to our Job class
  JobDetail printJobDetails = newJob(PrintJob.class).withIdentity("PrintJob", "PrintMessagesGroup")
    .usingJobData(jobDataMap).build();

  // Creating schedule time with trigger
  SimpleTrigger trigger = (SimpleTrigger) (newTrigger().withIdentity(triggerKey("myTrigger", "myTriggerGroup"))
    .withSchedule(simpleSchedule().withIntervalInSeconds(3).withRepeatCount(55))
    .startAt(futureDate(500, IntervalUnit.MILLISECOND)).withDescription("Job executes every 10 seconds")
    .usingJobData(triggerJobDataMap).build());

  // Start scheduler
  scheduler.start();

  Thread t1 = new Thread() {
   public void run() {

    while (true) {
     try {
      Thread.sleep(2000);
     } catch (InterruptedException e) {

     }
     System.out.println("---------------------------------------------------------");
     System.out.println("Repeat Count : " + trigger.getRepeatCount());
     System.out.println("Repeat Interval : " + trigger.getRepeatInterval());
     System.out.println("Number Of times triggered : " + trigger.getTimesTriggered());
     System.out.println("---------------------------------------------------------\n");

    }
   }
  };

  t1.start();

  // Schedule the jobs using triggers
  scheduler.scheduleJob(printJobDetails, trigger);

 }
}

Output
********************************************
Printing Job data map information
Description : This job prints some message to console

Printing Trigger Job data map information
userName : krishna123
token : myToken4321
********************************************
---------------------------------------------------------
Repeat Count : 55
Repeat Interval : 3000
Number Of times triggered : 0
---------------------------------------------------------
.....
.....

.....




Previous                                                    Next                                                    Home

No comments:

Post a Comment