Spring integrates quartz to realize timed task scheduling

Recently, the project needs to implement scheduled tasks, such as regularly calculating members' points, calling third-party interfaces, etc. because the project adopts the spring framework, it is introduced here in combination with the spring framework.

Write job class

That is, ordinary POJO, as follows:

Set specific tasks in the spring configuration file

be careful

A trigger can only trigger one job, but a job can be triggered by multiple triggers, which will cause concurrency problems. In quartz, if you don't want to execute the same job concurrently, you can implement stateful job instead of job. In spring, if the methodinvokingjobdetailfactorybean is used, it can be implemented by setting the concurrent = "false" attribute.

Endnote

The benefits of using quartz in spring instead of a single application include:

Putting all task scheduling settings in the same place makes the task easy to maintain.

Only the job is encoded. Trigger and scheduler can be set through configuration

You can use POJO Java beans to execute jobs without implementing the job interface

Detailed usage of cron expressions

Special characters allowed for field values

Seconds 0-59, - * / minutes 0-59, - * / hours 0-23, - * / dates 1-31, - */ L W C month 1-12 or Jan-Dec, - * / week 1-7 or sun-sat, - */ L C # year (optional) left blank, 1970-2099, - */

example:

0/5 * * * * ? : Every 5 seconds

The '' character is used to specify all values. For example, "every minute" means "every minute" in the field of minutes.

“?” Characters are only used in date and week fields. It is used to specify "ambiguous values". It is useful when you need to specify something in one of these two fields. Look at the example below and you will understand.

The date in the month and the date in the week are mutually exclusive. You should set a question mark to indicate that you don't want to set that field.

The '-' character is used to specify a range. For example, "10-12" in small time domain means "10 points, 11 points, 12 points".

The "," character is used to specify another value. For example, "Mon, wed, Fri" means "Monday, Wednesday and Friday" in the week field.

The "/" character is used to specify the increment. For example, "0 / 15" in the second field means 0, 15, 30 and 45 seconds per minute. "5 / 15" represents 5, 20, 35 and 50 per hour in the minute field. The symbol "" in front of "/" (such as: / 10) is equivalent to 0 in front of "/" (such as: 0 / 10). Remember the essence: each value field of an expression is a set with maximum and minimum values, such as: the set of second field and minute field is 0-59, the date field is 1-31, and the month field is 1-12. The character "/" can help you get the corresponding value in each character field. For example: "7 / 6" In the month field, it is triggered only in July, not every June.

L is the ellipsis of 'last'. It can represent the day of month and day of week fields, but the meaning in the two fields is different. For example, the day of month field represents the last day of a month. If it means' 7 'or' sat 'in the day of week field, if it is preceded by a number in the day of week field, it represents the last few days of a month. For example,' 6L 'represents the last Friday of a month.

The character "W" is only allowed in the date field. This character is used to specify the most recent working day of the date. For example, if you write "15W" in the date field, it means the latest working day on the 15th of this month. Therefore, if the 15th is Saturday, the task will be triggered on the 14th. If 15 is Sunday, the task will be triggered on Monday, that is, the 16th. If "1W" is filled in the date field, even if the 1st is Saturday, the task will only be triggered next Monday, that is, the 3rd. The latest working day specified by "W" character cannot cross months. The character "W" can only be used with a single value, not a number field. For example, 1-15w is wrong.

"L" and "W" can be used together in the date field. LW represents the working day of the last week of the month.

The character "#" is only allowed in the week field. This character is used to specify a day of the month. For example: "6#3" means Friday in the third week of the month (6 means Friday, 3 means the third week). "2#1" means Monday in the first week of the month. "4#5" means Wednesday in the fifth week.

The character "C" is allowed in the date field and the week field. This character depends on a specified "calendar". That is, the value of this expression depends on the calculation results of the related calendar. If there is no calendar Association, it is equivalent to all contained calendars. For example, if the date field is "5C", it means the first day in the associated "calendar" or the next 5 days of the first day of the month. The week field "1C" indicates the first day in the associated calendar, or the day after the first day of the week, that is, the day after Sunday (Monday).

Expression example

The above method of integrating quartz with spring to realize scheduled task scheduling is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

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
分享
二维码
< <上一篇
下一篇>>