Monday 29 July 2019

Quartz: Working with TriggerBuilder


Quartz provides a builder-style API for constructing jobs, Triggers, schedulers, dates (Ex: TriggerBuilder, JobBuilder, ScheduleBuilder, DateBuilder).

'org.quartz.TriggerBuilder' class is used to create Trigger instances.

Below statements create a simple trigger instance, that execute a job at the interval of 3 seconds indefinately.
SimpleTriggerImpl trigger = new SimpleTriggerImpl();
trigger.setStartTime(new Date(System.currentTimeMillis() + 500));
trigger.setRepeatCount(-1);
trigger.setRepeatInterval(3000);
trigger.setName("myTrigger");
trigger.setDescription("Job executes every 3 seconds");

We can rewrite the same using TriggerBuilder like below.

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

Trigger trigger = newTrigger()
         .withIdentity(triggerKey("myTrigger", "myTriggerGroup"))
         .withSchedule(
                  simpleSchedule()
                  .withIntervalInSeconds(3)
                  .repeatForever()
                  )
         .startAt(futureDate(500, IntervalUnit.MILLISECOND))
         .withDescription("Job executes every 3 seconds")
         .build();

PrintJob.java
package com.sample.jobs;

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

public class PrintJob implements Job {
 public void execute(JobExecutionContext jec) throws JobExecutionException {
  System.out.println("Printing.....");
 }
}


QuartzSchedulerEx.java
package com.sample.app;

import static org.quartz.DateBuilder.futureDate;
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.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.JobDetailImpl;
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();

  // Creating JobDetailImpl and link to our Job class
  JobDetailImpl printJobDetails = new JobDetailImpl();
  printJobDetails.setJobClass(PrintJob.class);
  printJobDetails.setName("PrintJob");
  printJobDetails.setGroup("PrintMessages");

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

  // Start scheduler
  scheduler.start();

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

 }
}

You can able to see below messages in console.

Printing.....
Printing.....
Printing.....
......

......


Previous                                                    Next                                                    Home

No comments:

Post a Comment