Quartz - DateIntervalTrigger trigger

Please reprint from the source: http://eksliang.iteye.com/blog/2208559

I. Overview

The internal implementation mechanism of simpleTrigger is to calculate the next execution time by calculating the interval time, which causes him to have timed tasks that are not suitable for scheduling. For example, we want to execute the task at 1:00AM every day. If SimpleTrigger is used, the interval is one day. Note that there is a problem here, that when there is a misfired task and it resumes execution, the execution time is random (depending on when the misfired task is executed, such as 3:00PM on a certain day). This will cause the execution time of each day to become 3:00PM instead of the 1:00AM we originally expected.

How should it be solved?

Answer: Use DateIntervalTrigger trigger

DateIntervalTrigger was added after Quartz 1.7 and is best for scheduling tasks like every N (1, 2, 3...) hours, every N days, every N weeks, etc. Although SimpleTrigger can also achieve similar tasks, DateIntervalTrigger will not be affected by the misfired task we mentioned above. In addition, DateIntervalTrigger will not be affected by DST (Daylight Saving Time, that is, China's Daylight Saving Time) adjustment. The author once changed the SimpleTrigger in the project to DateIntervalTrigger for this reason, because if SimpleTrigger is used, the originally set scheduling time will be advanced or delayed by an hour due to the adjustment of DST, and DateIntervalTrigger will not be affected by this.

 

2. Reference examples

Simple Job implementation class: print job details

 

package com.ickes.job;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
/**
 * Implement the Job interface and define the tasks to run
 * @author Ickes
 */
public class SimpleJob implements Job {

	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		//print task details
		System.out.println(
				context.getJobDetail().getGroup()
				+"——"+context.getJobDetail().getName()
				+"——"+context.getTrigger().getName()
				+"——"+context.getTrigger().getGroup());
	}

}

 

DateIntervalTriggerDemo test example

 

package com.ickes.job;

import org.quartz.DateIntervalTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
/**
 * @author Ickes
 */
public class DateIntervalTriggerDemo {
	public static void main(String[] args) throws Exception {
		//Step 1: Create a JobDetail instance
		JobDetail jobDetail = new JobDetail("j_job1","j_group1", SimpleJob.class);
		//Step 2: Define scheduling rules through DateIntervalTrigger triggers: schedule every 1 second
		/*
		 * The third parameter: DateIntervalTrigger.IntervalUnit.SECOND This is the interval unit
		 * The fourth parameter: how many times the third parameter unit triggers a job
		 * For example: the following is triggered every second
		 */
		DateIntervalTrigger trigger = new DateIntervalTrigger("t_trigger1","t_group1",
				DateIntervalTrigger.IntervalUnit.SECOND, 1);
		//Step 3: Get a scheduler instance through SchedulerFactory
		SchedulerFactory schedulerFactory = new StdSchedulerFactory();
		Scheduler scheduler = schedulerFactory.getScheduler();
		//Step 4: Register the job and trigger to the scheduler for scheduling
		scheduler.scheduleJob(jobDetail, trigger);
		//Step 5: Schedule startup
		scheduler.start();
	}
}

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326353915&siteId=291194637