Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

@ Scheduled comment

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

1 Overview

@ Scheduled annotations are annotations provided by spring boot for scheduled task control, which are mainly used to control the execution of tasks at a specified time or at regular intervals. Note that it needs to be used with @ EnableScheduling. There are three main ways to configure @ Scheduled to configure execution time, cron,fixedRate,fixedDelay.

2 cron

Cron is a parameter of @ Scheduled, a string separated by 5 spaces, only 6 fields are allowed (note that not 7, 7 will directly report an error), representing seconds, minutes, hours, days, months, and weeks, respectively.

Unit allowable values allow wildcard characters 0-59 seconds,-* / minutes 0-59,-* / hour 0-23,-* / day 1-31,-* /? LW month 1-12 or JAN-DEC (uppercase and lowercase),-* /? Monday-7 or SUN-SAT (uppercase and lowercase),-* /? L # 2.1 cron wildcard symbol meaning * all values. Represents execution per second on the second field and monthly execution on the month field? Do not specify a value, do not need to relate to the value of the currently specified field, for example, execute every day but do not need to care about the day of the week, you can set the field of the week to?-range, such as 0-2 of a second, indicating that 0 second, 1 second, 2 seconds will trigger, multiple values, such as 0 second, 20 seconds, 25 seconds trigger, you can set the second field to 0, 20, 25 / incremental trigger, for example, set 0jump 3 on the second field, starting from 0 second When L is triggered every 3 seconds, L is only allowed on the day field or week field, L is used on the day field to indicate the last day of the month, and 3L is used on the week field to indicate that the last Thursday of the month W is only allowed to be used in the day field. Represents the working day closest to that day. Working days refer to Monday to Friday # only allowed on the week field, indicating the day ordinal of each month, such as 2: 3, 2.2 cron example @ Scheduled (cron = "0 * 1 SAT") / / all 0 seconds of all Saturdays in January of each year @ Scheduled (cron = "001 Jan?") / 00:00:00 on January 1st of each year

Cron supports placeholders if there is in the configuration file

Cron = 22 2 2 2?

Then

@ Scheduled (cron = "${cron}")

It means that it will be carried out at 02:02:02 on February 2 every year.

3 fixedRate3.1 fixedRate

FixedRate indicates how long it has been executed since the last execution time, in ms.

Such as

@ Scheduled (fixedRate = 1000 * 5)

Execute again 5 seconds after the last execution.

3.2 fixedRateString

There is a similar parameter called fixedRateString, which is in the form of a string and supports placeholders.

Such as

@ Scheduled (fixedRateString = "1000")

Execute again for 1 second since the last execution.

If there are corresponding properties in the configuration file, you can use placeholders to get the properties, such as in application.properties

Interval=2000

Can be used

@ Scheduled (fixedRateStirng= "${interval}")

Represents a 2-second interval.

4 fixedDelay4.1 fixedDelay

FixedDelay is a bit similar to fixedRate, except that fixedRate is timed after the last start, and fixedDelay is timed after the last finish, that is, fixedDelay indicates how long it will take to execute after the last execution, also in ms.

@ Scheduled (fixedDelay = 1000 * 3600 * 12) / / execute 4.2 fixedDelayString again half a day after the last execution

Similar to fixedRateString, placeholders are also supported

@ Scheduled (fixedDelayString = "${fixedDelay}") 5 initialDelay5.1 initialDelay

InitialDelay indicates how long the execution will be delayed for the first time (in ms), and then it will be executed according to the rules specified by cron/fixedRate/fixedRateString/fixedDelay/fixedDelayString. One of the rules needs to be specified.

@ Scheduled (initialDelay=1000,fixedRate=1000) / / first run deferred 1s5.2 initialDelayString

Similar to initialDelay, it is only a string and supports placeholders.

@ Scheduled (initialDelayString = "${initialDelay}", cron = "0.0014 4?") / / delayed for the first time according to the time specified by the configuration file initialDelay, and executed at 00:00:00 on April 14 every year

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report