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】

[java small class of Xiuzhen academy] task scheduling - quartz

Opening remarks:

Hello, I'm Liao you, a student of the 32nd session of the Beijing Branch of it Academy. I'm an honest, pure and kind java programmer. Today, I'd like to share with you the knowledge point of Java task 10 on the official website of the Academy - task scheduling - quartz

1、 Background:

1. Task scheduling concept

Task scheduling refers to the automatic execution of tasks based on a given time point, given time interval or given execution times.

2. Task scheduling application scenario

Scene 1: get together and grab the red envelope on the whole point

Scenario 2: Double 11 real-time statistics of transaction volume

The above two scenarios can be realized through scheduled tasks, i.e. executing tasks at a specified time and at a fixed frequency.

3. Several Java implementations of task scheduling

Timer

ScheduledExecutor

Spring Scheduled

Open Source Toolkit quartz

Open Source Toolkit jcrontab

2、 Knowledge analysis:

1. Quartz core concepts

Task job: what do you do

Trigger: when does it work

Scheduler: integrate job and trigger together to create a job schedule, that is, when to do what

2. Several core interfaces of quartz

Job is an interface with only one execute method. When defining a job, you need to implement the execute method of this interface.

JobDetail: JobDetail is an interface. Through the implementation class of JobDetail, you can set specific execution jobs and set names, groups and parameters for the executed jobs.

Trigger: trigger is an interface used to set the time trigger rules that trigger job execution. There are two implementation classes: simpletrigger and crontrigger. Simpletrigger is applicable to single execution or fixed cycle scheduled task execution. Cronrigger can define scheduling plans of various complex time rules through cron expression.

Scheduler: scheduler is an interface that provides operations such as starting, stopping, resuming and deleting job plans. It creates a job plan through JobDetail and trigger.

3. Cron expression

* * * * * * *

It consists of seven parts, from left to right: seconds, minutes, hours, days, months, weeks and years. One year is optional, and it is generally not used.

Special symbols:

Asterisk (*): it can be used in all fields to represent each time in the corresponding time domain. For example, in the minute period, it means "every minute". It can be understood as a wildcard to configure all times.

Question mark (?): Used only in the date and week fields, it is usually specified as "meaningless value".

Minus sign (-): indicates a range. For example, "10-12" is used in the hour field to indicate from 10 o'clock to 12 o'clock, that is, 10 11 12.

Comma (,): indicates a list value. For example, "5,7,10" in the hour field indicates 5:00, 7:00 and 10:00.

(/): indicates the interval time

4. Cron exercise

"0 0 12 * * ?" Triggered at 12 noon every day

"0 * 14 * * ?" Triggered every 1 minute between 2 p.m. and 2:59 p.m. every day

"0 0/5 14 * * ?" Triggered every 5 minutes between 2 p.m. and 2:55 p.m. every day

"0 0-5 14 * * ?" Triggered every 1 minute between 2 p.m. and 2:05 p.m. every day

Generate cron expressions online: http://cron.qqe2.com/

5. Spring boot integrates quartz

3、 Coding practice

4、 References:

[1] https://www.ibm.com/developerworks/cn/java/j-lo-taskschedule/

[2] http://www.quartz-scheduler.org/

5、 More discussion:

Q1: what are the differences between several Java implementations of task scheduling?

A1: timer is the task scheduler added by JDK at the beginning, but it can only run in a single thread, that is, each task is serial and can only execute one task at a time. Therefore, Java later developed scheduledexecurator. Spring scheduled comes with spring and is easy to use. It only needs to add annotations. It is especially suitable for simple tasks. Quartz is a special task scheduling framework, which is suitable for complex task scheduling and distributed scheduling.

Q2: when using cron expression, what happens if the set time is not in the field range?

A2: an exception will be thrown and a prompt will be given when the program is running.

Q3: why is it recommended to omit the last bit for cron expressions?

A3: first, the last digit indicates the year, which is not commonly used in projects. Second, in some environments, seven digit cron expressions are not supported, and only six digit cron expressions are supported.

6、 Conclusion:

That's all for today's sharing. You are welcome to like, forward, leave messages and make bricks~

@L_ 419_ 0 @ 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
分享
二维码
< <上一篇
下一篇>>