Explain the implementation method of spring boot timing task in detail

Recently, I encountered a problem when writing a timing project with springboot, that is, the result of the client accessing the server actually changes once every period of time, and the server triggers an event at a fixed time point every day.

Of course, we can recalculate the result when encountering each request, but in order to improve efficiency, we can obviously let the server calculate the result every other period of time, save the result, and directly return the calculated result for each request in the next period of time. This can better improve the performance of the server.

Then the problem is how to deal with scheduled tasks. In fact, springboot has long provided a very convenient interface, but the online introduction is still a little messy. I'll record the precautions for specific operations for easy search in the future.

Create a scheduled service

Generally speaking, the timing service is written in a component to facilitate management. For scheduled tasks, we just need to add the @ scheduled annotation in front of the functions that need to be executed regularly, such as the following:

At the same time, we also need to configure the @ enableshcheduling annotation in the project startup file to tell the project that we support scheduled tasks:

So our function can execute regularly.

Scheduled parameters

Scheduled mainly supports fixrate, fixdelay, cron and initialdelay parameters, which are briefly described below.

Fixrate and fixdelay

The fixrate and fixdelay parameters both specify that the function is executed every certain number of milliseconds, but there are small differences between them.

fixRate

The timing of fixrate is relative to the system time, that is, it will be executed at a fixed time.

fixDelay

The timing of fixdelay is relative to the time of the last call, so it is affected by other program calls. If the function is manually called elsewhere, the timer will be re timed.

Initialdelay parameter

The initialdelay parameter is an additional parameter, which is relatively simple. It specifies the execution time from the start of the project to the first call of the function, in milliseconds.

If this parameter is not specified, the value is - 1, that is, the program will not be executed at the beginning.

When I don't know the parameter initialdelay, in order to call this function immediately when the program starts, I let this timing class inherit initializingbean and manually call this function in the overridden afterpropertieset method... It looks stupid now...

cron

Cron is the most complex and highly customized timing tool. There are similar crontab commands in Linux system. In fact, it defines the timing task in more detail and represents it in the form of a string.

In springboot, a cron string is a string composed of six parts with spaces. The examples in the document are as follows:

The six parts represent seconds, minutes, hours, days, months and weeks respectively.

It supports' - 'to represent the range;' * 'to represent the universal configuration;' / 'to represent the time interval on the right after the time on the left is matched;'? ' Generally, it refers to the general configuration of the week.

Refer to relevant documents for specific use methods.

There are many test tools like cron expression test tools on the Internet, which is convenient for us to test our cron expressions.

The following example shows that it is executed every five minutes:

Attention

When actually using the scheduled annotation, we must specify and only specify one of fixrate, fixdelay and cron, otherwise an error will be caused. Of course, we can specify the initialdelay parameter arbitrarily.

The above is the implementation method of spring boot timing task introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time!

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