quartz。

This is the back-end small class of the monastery. Each article is shared from

[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]

Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:

【quartz】

Hello, I am the 13th student of Shenzhen Branch of it Academy. I am an honest, pure and kind java programmer. Today, I'd like to share with you an extension of Java task 4 on the official website of the Academy: quartz.

1 background 1.1 what is quartz?

Quartz is a task scheduling library written in Java, which is open-source organized by OpenSymphony. Most companies use the task scheduling function. For example, companies need to perform task scheduling regularly to generate reports, or regularly update blogs, which can be completed by quartz. Task scheduling: there are now n tasks (program) is required to be executed at a specified time, such as task a at 3:00 every Tuesday, Task B at an interval of 5S every day, etc. this multi task has a variety of execution strategies, which is task scheduling. The core role of quartz is to make task scheduling rich, efficient and safe. Developers only need to adjust a few quartz interfaces and make simple configuration to achieve the above requirements.

2 knowledge analysis

2.1 architecture of quartz

1. Job: it is an interface with only one method void execute (jobexecutioncontext). Developers implement this interface to define running tasks. Jobexecutioncontext class provides various information of scheduling context. The job runtime information is saved in the jobdatamap instance;

2. JobDetail: Quartz recreates a job instance every time it executes a job, so it does not directly accept an instance of a job. Instead, it receives a job implementation class so that the runtime can instantiate a job through the reflection mechanism of newinstance(). Therefore, it is necessary to use a class to describe the job implementation class and other related static information, such as job name, description, associated listener and so on. JobDetail assumes this role.

3. Trigger: a class that describes the time trigger rules that trigger job execution. There are mainly two subclasses: simpletrigger and crontrigger. Simpletrigger is the most suitable choice when it only needs to be triggered once or executed periodically at fixed time intervals; Cronrigger can define various scheduling schemes of complex time rules through cron expression: such as 9:00 every morning, 5:00 on Monday, Wednesday and Friday, etc;

4、Calendar:org. quartz. Calendar and Java util. Calendar is different, It is a collection of calendar specific points in time (you can simply think of org.quartz.calendar as a collection of java.util.calendar - java.util.calendar represents a calendar time point). A trigger can be associated with multiple calendars to exclude or include some time points. Suppose that we schedule tasks to be executed at 10:00 a.m. on Monday every week, but if we encounter a legal holiday, the tasks will not be executed, which is necessary Based on the trigger mechanism, calendar is used for fixed-point exclusion. For different time period types, quartz at org quartz. impl. Several calendar implementation classes are provided under the calendar package, such as annualcalendar, monthlycalendar and weekly calendar, which are defined for each year, month and week respectively;

5. Scheduler: represents a quartz independent running container. Trigger and JobDetail can be registered in the scheduler. They have their own groups and names in the scheduler. Groups and names are the basis for the scheduler to find and locate an object in the container. Trigger groups and names must be unique, The group and name of JobDetail must also be unique (but it can be the same as trigger's group and name because they are different types). Scheduler defines multiple interface methods to allow external access and control of trigger and JobDetail in the container through groups and names.

2.2 functions of hystrix: 1. Protect call delay and errors between systems, especially network calls through third-party tools

2. Prevent error propagation before distributed systems

3. Rapid failure and rapid recovery

4. Error fallback and elegant service degradation

3. Frequently asked questions

After the program restarts, the task disappears

4 Solutions

Persistence of quartz tasks using JDBC

5. Coding practice

6. Expand thinking

7. References: CSDN, Baidu Encyclopedia

8 more discussion 1. How many storage methods are there in quartz?

RAMJobStore, jobstoresupport, where RAMJobStore stores triggers and jobs in memory, and jobstoresupport stores triggers and jobs in the database based on JDBC. RAMJobStore has very fast access speed, but because all data will be lost after the system is stopped, jobstoresupport is used in common applications.

2. What is the difference between a job in quartz and a stateful job?

Job: for ordinary tasks, or stateless tasks, the status will not be recorded after the JobDetail is executed.

Statefuljob: inherited from job, because the @ persistjobdataafterexecution annotation was added during definition. For the same trigger, stateful jobs cannot be executed in parallel. The next execution can only be triggered after the last triggered task is executed.

3. In addition to quartz, the implementation of other scheduled tasks?

Java has its own classes timer and TimerTask. Timer class is used to record time and timing. It accepts a TimerTask as a parameter; TimerTask can write our tasks.

Spring3. The task built after 0 can be regarded as a lightweight quartz

Skill tree It Academy

"We believe that everyone can become an engineer. From now on, find a senior brother to introduce you, control your learning rhythm, and stop being confused on the way to learning.".

Here is the skill tree In it academy, thousands of senior brothers have found their own learning route here. Learning is transparent and growth is visible. Senior brothers have 1-to-1 free guidance.

Come and study with me~

Tencent Video:

Ppt link video link

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>