Monday, 29 July 2019

Quartz: CronTrigger: Get the time zone


'com.quartz.CronTrigger' interface provides 'getTimeZone' method, it returns the time zone for which the cronExpression of
this CronTrigger will be resolved.

Example
TimeZone timeZone = cronTrigger.getTimeZone();

Find the below working example.

PrintJob.java
package com.sample.jobs;

import java.util.Map.Entry;
import java.util.TimeZone;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.impl.triggers.CronTriggerImpl;

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());
  }

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

  String jobName = cronTrigger.getJobName();
  String jobGroup = cronTrigger.getJobGroup();
  String triggerName = cronTrigger.getName();
  String triggerGroup = cronTrigger.getGroup();

  String cronExpresion = cronTrigger.getCronExpression();
  TimeZone timeZone = cronTrigger.getTimeZone();
  String expressionSummary = cronTrigger.getExpressionSummary();

  String jobInfo = String.format("Trigger '%s' from group '%s' executing the job '%s' of group '%s'", triggerName,
    triggerGroup, jobName, jobGroup);

  System.out.println(jobInfo);
  System.out.println("Time Zone : " + timeZone);
  System.out.println("expressionSummary : " + expressionSummary);
  System.out.println("Cron Expression : " + cronExpresion);
  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.TriggerBuilder.newTrigger;
import static org.quartz.TriggerKey.triggerKey;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
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.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", "PrintJobMessagesGroup")
    .usingJobData(jobDataMap).build();

  // Creating schedule time with trigger
  CronTrigger trigger = (CronTrigger) (newTrigger().withIdentity(triggerKey("myTrigger", "myTriggerGroup"))
    .withSchedule(CronScheduleBuilder.cronSchedule("0/3 * * * * ?"))
    .startAt(futureDate(500, IntervalUnit.MILLISECOND)).withDescription("Job executes every 3 seconds")
    .usingJobData(triggerJobDataMap).build());
  

  scheduler.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
Trigger 'myTrigger' from group 'myTriggerGroup' executing the job 'PrintJob' of group 'PrintJobMessagesGroup'
Time Zone : sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=7,lastRule=null]
expressionSummary : seconds: 0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57
minutes: *
hours: *
daysOfMonth: *
months: *
daysOfWeek: ?
lastdayOfWeek: false
nearestWeekday: false
NthDayOfWeek: 0
lastdayOfMonth: false
years: *

Cron Expression : 0/3 * * * * ?
********************************************
......
......

......




Previous                                                    Next                                                    Home

No comments:

Post a Comment