Implement quartz timer and quartz timer principle introduction

  • 2020-04-01 02:35:31
  • OfStack

I. core concepts
 
The principle of Quartz is not very complicated, just to understand a few concepts and know how to start and close a scheduler.

1, the Job
Represents a job, the specific content to be performed. There is only one method in this interface
Void the execute (JobExecutionContext context)

2, JobDetail
JobDetail represents a concrete executable scheduler. Job is what the scheduler executes. In addition, JobDetail contains the scheme and policy of the task scheduling.

3. Trigger represents the configuration of a scheduling parameter, when to be adjusted.

A Scheduler represents a scheduling container in which multiple JobDetail and triggers can be registered. When the Trigger is combined with the JobDetail, it can be scheduled by the Scheduler container.

Two, one of the simplest introductory examples


import org.quartz.*; 
import org.quartz.impl.StdSchedulerFactory;
import java.util.Date;
 
public class MyJob implements Job { 
        public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { 
                System.out.println(new Date() + ": doing something..."); 
        } 
}
class Test { 
        public static void main(String[] args) { 
                //1. Create a job round
                JobDetail jobDetail = new JobDetail(); 
                //Set work item
                jobDetail.setJobClass(MyJob.class); 
                jobDetail.setName("MyJob_1"); 
                jobDetail.setGroup("JobGroup_1");
                //2. Create the Trigger object
                SimpleTrigger strigger = new SimpleTrigger(); 
                strigger.setName("Trigger_1"); 
                strigger.setGroup("Trigger_Group_1"); 
                strigger.setStartTime(new Date()); 
                //Set the repeat stop time and destroy the Trigger object
                java.util.Calendar c = java.util.Calendar.getInstance(); 
                c.setTimeInMillis(System.currentTimeMillis() + 1000 * 1L); 
                strigger.setEndTime(c.getTime()); 
                strigger.setFireInstanceId("Trigger_1_id_001"); 
                //Set the repeat interval time
                strigger.setRepeatInterval(1000 * 1L); 
                //Set the number of repeats
                strigger.setRepeatCount(3);
                //3. Create a Scheduler object and configure the JobDetail and Trigger objects
                SchedulerFactory sf = new StdSchedulerFactory(); 
                Scheduler scheduler = null; 
                try { 
                        scheduler = sf.getScheduler(); 
                        scheduler.scheduleJob(jobDetail, strigger); 
                        //4. And perform startup, shutdown and other operations
                        scheduler.start();
                } catch (SchedulerException e) { 
                        e.printStackTrace(); 
                } 
//                try { 
//                        // Turn off the scheduler  
//                        scheduler.shutdown(true); 
//                } catch (SchedulerException e) { 
//                        e.printStackTrace(); 
//                } 
        } 
}

Execution results:


When the end time is changed to:


//Set the repeat stop time and destroy the Trigger object
java.util.Calendar c = java.util.Calendar.getInstance(); 
c.setTimeInMillis(System.currentTimeMillis() + 1000 * 1L); 
strigger.setEndTime(c.getTime());

Execution results:

When adding a statement to close the scheduler:
//4, and perform startup, shutdown and other operations
The scheduler. The start ();
The scheduler. Shutdown (true);


Program execution results:
Thu Jul 23 10:11:50 CST 2009: doing something...

Process finished with exit code 0
It was executed only once, and this time it was completed because the scheduler.shutdown(true) was set; True stands for waiting for the task to complete.

You can also see from here that the scheduler is a container, and the scheduler controls the execution of the jobDetail, and the policy of control is through the trigger.

When the scheduler container is started, the jobDetail can be executed based on the associated trigger policy. When the scheduler container is closed, all jobDetail stops executing.

Third, through the example to see the principle

Through the study of Quartz source code, and this example, finally understand the working principle of Quartz.

1. A scheduler is a scheduler container (headquarters) that can hold numerous jobdetails and triggers. When the container is started, each JobDetail in the container is automatically executed according to the trigger.

2. JobDetail is an executable job that may itself be stateful.

3. Trigger represents the configuration of a scheduling parameter, when to be adjusted.

4. When the JobDetail and Trigger are registered on the scheduler container, an assembled job (a pair of JobDetail and Trigger) is formed and can be scheduled for execution with the container being started.

5. The scheduler is a container with a thread pool in it that is used to schedule the execution of each job in parallel to improve container efficiency.

6. The above structure is represented by a diagram as follows:
 


Four,

1. Made clear the principle and process of executing jobs on the Quartz container, the way of job formation, and the method of job registration to the container. You understand the core principle of Quartz.

2. Although Quartz is huge, everything revolves around this core. In order to configure a powerful time scheduling policy, special CronTrigger can be studied. To configure jobs and container properties flexibly, you can do this through the Quartz properties file or through XML.

3. To schedule more persistent, structured jobs, read the jobs from the database and execute them in the container.

4. Everything revolves around this core principle. Once you understand this, it's much easier to move on to more advanced USES.

5, Quartz, and the integration of the Spring is also very simple, Spring provides a set of beans to support: MethodInvokingJobDetailFactoryBean, SimpleTriggerBean, SchedulerFactoryBean, see what needs to be injected inside property can be understood. Spring starts the Quartz container when the Spring container starts.

6. The closure of the Quartz container is also simple. For Spring consolidation, there are two methods: one is to close the Spring container, and the other is to get the SchedulerFactoryBean instance and then call a shutdown. If you use Quartz on its own, the scheduler.shutdown(true) is called directly;

7. JobDetail and Trigger of Quartz can be reset at run time and will work on the next call. This provides a basis for the implementation of dynamic jobs. You can store the scheduling policy in the database and then set the Trigger from the database data to generate dynamic scheduling.


Related articles: