Quartz source Code Analysis--Turn

Source: Internet
Author: User

Quartz Source Code Analysis (a),

http://ssuupv.blog.163.com/blog//146156722013829111028966/

Any individual, any enterprise, any industry will have job scheduling needs. Give a few simple examples:

1, every month will pay, every month to be reimbursed, etc.;

2, everyone to eat and sleep every day;

3, each company has the accounting period, all need to report to the company leaders Monthly, quarterly, annual report and so on.

4, banking and financial industry daily need to cut and so on.

For individuals, the perception of job scheduling is not very clear, but for an enterprise, a good job scheduling can save a lot of time and money for the enterprise. As the saying goes, time is money, too much resources into the boring task is undoubtedly a waste of money and resources, with the complexity of business processes, automated processes and automated job scheduling to highlight the benefits. The reason why automation occurs, because in the repetitive work, people's efficiency and accuracy is much lower than the computer. Automatically arrange a series of tasks into a job, and then create a scheduler for the job, and the job will be executed automatically.

Quartz is a classic job scheduling framework, at the beginning of in-depth research, first imitate the principle of quartz, design a simple job scheduler, the class diagram is as follows:

1, first look at the job class, this class, very simple, there is only one execute method, this method is the job specific execution of content:

2, Jobdetail class, the class is the encapsulation of the specific job class, including JobName (ID), job execution required run-time parameters, in the HashMap named Jobdata

3. Trigger class, record the time of the next run of the job and the key of the running job:

4, Scheduler class, the most important class, used to start and stop the framework

5, scheduler execution is executed in the scheduler Schedulerthread; the most important thing in a thread is the Run method body, and there is a halt method to stop the thread. First look at the halt method

The Run method body is:

All the framework code is now complete. Note: Nullpointexception and multithreading issues are not considered.

Then look at the test code:

Results

The system executes at the next run time and displays the results successfully.

This chapter simply extracts the basic IQ of the core processing logic of quartz, removing the fundamental scheduling tasks written for security verification and multithreading synchronization issues. Only for the quartz of the source code for the interpretation of a foundation.

Quartz Framework Analysis (II.)

http://ssuupv.blog.163.com/blog/static/146156722013829324461/

The tasks performed by the quartz framework need to be written in a class that implements the job interface, perform the related operations in execute (jobexecutioncontext context) in the implementation class, and implement the job interface to make your Java Components can be easily executed by scheduler, the following is the job interface:

As above, you will see that the job's Execute method will be executed when the job trigger is started. So, scheduler's job is created and executed in what way. First look at the job deployment, the job is through the Jobdetail Scheduler to the framework, and then through the Getjobclass () method to get to the job implementation class of classes, through Jobfactory newjob () The Jobrunshell method reflects a specific instance of Java and executes in the run method of the thread object.

1, first look at the creation of Jobdetail, in quartz2.0, Jobdetail is created through Jobbuilder

Jobbuilder creates the Jobbuilder by using a static method Newjob () different overloads, setting the parameters through the withidentity () overload, as follows:

2, after creating the number Jobdetail, scheduler through the thread class Jobrunshell to perform the job.

The job instance is created by calling Jobfactory's Newjob () in the Initialize method.

The Jobfactory Newjob () method implements the job instance creation by reflection using the default parameterless construction method, as follows:

Job execution is straightforward, just call the Execute method, and the following code omits the intermediate check section:

Job implementation and execution is simple, mainly business logic, the following simple to sort out the relationship between the classes:

A brief analysis of Quartz source code (III.)

http://ssuupv.blog.163.com/blog/static/14615672201382931053129/

Quartz Trigger Part

Trigger is used to record information such as the run time, number of runs, etc. of the job task. The basic class diagram for trigger is as follows:

1, trigger interface mainly provides trigger necessary parameter information, such as Triggerkey, in the trigger interface only provides jobkey,key and other information of the Get method, and there is no set method, The trigger interface then derives a Mutabletrigger interface that provides a way to set the values of the above fields. Other sub-interfaces of trigger Crontrigger and Simpletrigger have different forms of parametric methods for different types of trigger based on the basic trigger methods provided.

2. All trigger implementation classes are inherited from Abstracttrigger,abstracttrigger and do not provide any special method body, Only the parameters corresponding to the Get method for the trigger interface and the set method of the Mutabletrigger interface are declared as private variables.

3, there are 3 quartz in the trigger. Crontriggerimpl, Simpletriggerimpl and Calendaritervaltrigger. The calculation is mainly for time.

Generally speaking, trigger is still relatively simple.

Quartz source Code Analysis--Turn

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.