Distributed quartz timing service and principle of quartz timer

A picture illustrating the core of Quartz

 1. Core concepts

1. Job
represents a job and the specific content to be executed.
There is only one method void execute(JobExecutionContext context) in this interface.

2. JobDetail
JobDetail represents a specific executable scheduler. Job is the content to be executed by the executable scheduler. In addition, JobDetail also includes the scheme and strategy of this task scheduling.

3. Trigger represents the configuration of a scheduling parameter, when to adjust it.

4. Scheduler represents a scheduling container, and multiple JobDetails and Triggers can be registered in a scheduling container. When the Trigger is combined with the JobDetail, it can be scheduled by the Scheduler container.

Two, demo

import org.quartz.*; 
import org.quartz.impl.StdSchedulerFactory;

 

import java.util.Date;


//quartz定时器测试 

public class MyJob implements Job { 
        public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { 
                System.out.println(new Date() + ": doing something..."); 
        } 
}

class Test001 { 
        public static void main(String[] args) { 
                //1、创建JobDetial对象 
                JobDetail jobDetail = new JobDetail(); 
                //设置工作项 
                jobDetail.setJobClass(MyJob.class); 
                jobDetail.setName("MyJob_1"); 
                jobDetail.setGroup("JobGroup_1");

                //2、创建Trigger对象 
                SimpleTrigger strigger = new SimpleTrigger(); 
                strigger.setName("Trigger_1"); 
                strigger.setGroup("Trigger_Group_1"); 
                strigger.setStartTime(new Date()); 
                //设置重复停止时间,并销毁该Trigger对象 
                java.util.Calendar c = java.util.Calendar.getInstance(); 
                c.setTimeInMillis(System.currentTimeMillis() + 1000 * 1L); 
                strigger.setEndTime(c.getTime()); 
                strigger.setFireInstanceId("Trigger_1_id_001"); 
                //设置重复间隔时间 
                strigger.setRepeatInterval(1000 * 1L); 
                //设置重复执行次数 
                strigger.setRepeatCount(3);

                //3、创建Scheduler对象,并配置JobDetail和Trigger对象 
                SchedulerFactory sf = new StdSchedulerFactory(); 
                Scheduler scheduler = null; 
                try { 
                        scheduler = sf.getScheduler(); 
                        scheduler.scheduleJob(jobDetail, strigger); 
                        //4、并执行启动、关闭等操作 
                        scheduler.start();

                } catch (SchedulerException e) { 
                        e.printStackTrace(); 
                } 
//                try { 
//                        //关闭调度器 
//                        scheduler.shutdown(true); 
//                } catch (SchedulerException e) { 
//                        e.printStackTrace(); 
//                } 
        } 
}
当添加一条关闭调度器的语句:
并执行启动、关闭等操作 
scheduler.start(); 
scheduler.shutdown(true);

3. Examples

1. The scheduler is a planning scheduler container (headquarters). The container can contain many JobDetails and triggers. When the container is started, each JobDetail inside will be automatically executed step by step according to the trigger.

2. JobDetail is an executable job, which itself may be stateful.

3. Trigger represents the configuration of a scheduling parameter, when to adjust it.

4. After the JobDetail and Trigger are registered on the scheduler container, an assembled job (a pair of JobDetail and Trigger) is formed, which can be scheduled and executed along with the startup of the container.

5. The scheduler is a container, and there is a thread pool in the container, which is used to schedule and execute each job in parallel, which can improve the efficiency of the container.

Four. Summary

1. I have figured out the principle and process of executing jobs on the Quartz container, as well as the way jobs are formed and how jobs are registered to the container. I understand the core principles of Quartz.

2. Although Quartz is huge, everything revolves around this core. In order to configure a powerful time scheduling strategy, you can study a special CronTrigger. To flexibly configure job and container properties, it can be implemented through Quartz's properties file or XML.

3. If you want to schedule more persistent and structured jobs, you can read the jobs through the database, and then execute them in the container.

4. Everything revolves around this core principle. Once you understand this, it will be much easier to study more advanced usages.

5. The integration of Quartz and Spring is also very simple. Spring provides a set of beans to support: MethodInvokingJobDetailFactoryBean, SimpleTriggerBean, SchedulerFactoryBean, just look at what attributes need to be injected into it. Spring will start the Quartz container when the Spring container starts.

6. The shutdown method of the Quartz container is also very simple. If it is Spring integration, there are two methods, one is to close the Spring container, and the other is to obtain the SchedulerFactoryBean instance, and then call a shutdown to get it done. If Quartz is used independently, call scheduler.shutdown(true) directly;

7. Quartz's JobDetail and Trigger can be reset at runtime and will take effect the next time it is called. This provides a basis for the realization of dynamic operations. You can store the scheduling time policy in the database, and then set the Trigger through the database data, so that dynamic scheduling can be generated.

Guess you like

Origin blog.csdn.net/FDX0821/article/details/125432446
Recommended