In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what is the @ Scheduled parameter and cron expression". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the @ Scheduled parameter and cron expression"!
The @ Scheduled parameter and cron expression interpretation @ Scheduled supports the following 8 parameters
1.cron: expression that specifies that the task executes at a specific time
2.fixedDelay: indicates how long the task will be executed again after the last task is completed. The parameter type is long, in ms.
3.fixedDelayString: the same meaning as fixedDelay, except that the parameter type changes to String
4.fixedRate: indicates that tasks are executed at a certain frequency, that is, the interval between each start of execution is the same. The parameter type is long (in ms).
5.fixedRateString: the same meaning as fixedRate, except that the parameter type is changed to String
6.initialDelay: indicates how long the delay will be before the task is executed for the first time. The parameter type is long, in ms.
7.initialDelayString: the same meaning as initialDelay, except that the parameter type is changed to String
8.zone: time zone. Default is the current time zone.
The cron expression is a string with 6 fields separated by spaces
[seconds] [minutes] [hours] [day] [month] [week]
Cron expressions for scheduled tasks included with spring do not support years.
As shown in the figure:
The following source code is available in the above figure class:
Private void parse (String expression) throws IllegalArgumentException {String [] fields = StringUtils.tokenizeToStringArray (expression, "); if (! areValidCronFields (fields)) {throw new IllegalArgumentException (String.format (" Cron expression must consist of 6 fields (found% d in\ "% s\"), fields.length, expression));} doParse (fields) } private static boolean areValidCronFields (String [] fields) {return (fields! = null & & fields.length = = 6);}
The expression parameter in the code is the cron expression, so the correct cron expression in the scheduled task that comes with spring can only be 6 fields, otherwise the IllegalArgumentException prompt will be thrown: Cron expression must consist of 6 fields (the cron expression must be composed of 6 fields).
Field required values allowed wildcard seconds (seconds) are 0-59 integers,-* / minute (minutes) is 0-59 integers,-* / hour (hours) is 0-23 integers,-* / day (daysOfMonth) is 1-31 integers (days need to be taken into account),-*? / L W month (months) is 1-12 integers or JAN-DEC -* / week (daysOfWeek) is a 1-7 integer or SUN-SAT,-*? / L # wildcard description
*: indicates that any value matches the field. Use * in the details domain to indicate every minute. In months, it means every month. The daysOfWeek domain represents each day of the week.
?: can only be used in daysofMonth and daysofWeek fields to indicate that no value is specified. When one of the two subexpressions is assigned a value, to avoid conflicts, you need to set the value of the other subexpression to?. Because daysofMonth and daysofWeek influence each other. For example, if you want to trigger the schedule on the 2nd of each month, no matter what day it is, you can only use the following words: 002 *, where the last bit can only use?, but not *. If you use *, it will be triggered regardless of the day of the week.
-: represents the range. For example, use 5-20 in the details domain, which means that it is triggered every minute from 5 minutes to 20 minutes.
/: indicates that the start time starts to trigger, and then triggers at regular intervals. For example, using 5max 20 in the details domain means that it is triggered every 20 minutes from the fifth minute of the current hour.
Indicates that enumerated values are listed For example, using 5 minute 20 in the details field means triggering once at 5 and 20 respectively.
L: finally, it is the abbreviation of the word "last", which can only appear in the daysofWeek and dayof Month fields. Using 5L in the daysofWeek domain means to trigger on the last Thursday of the specified month. Using 5L or FRIL in the dayofMonth field means to trigger on the penultimate day of the specified month. Do not specify a list or range when using the L parameter.
W: indicates a valid working day (Monday to Friday), which can only occur in the daysofMonth domain, and the system will trigger the event on the nearest valid working day from the specified date. For example, if 5W is used in daysofMonth, if the 5th is a Saturday, it will be triggered on the nearest working day Friday, that is, the 4th. If the 5th is Sunday, it will be triggered on the 6th (Monday). If the 5th is one of the days from Monday to Friday, it will be triggered on the 5th. In addition, W's recent search will not cross the month.
LW: these two characters can be used together to indicate the last working day of a specified month.
#: used to determine the day of the month, and can only appear in the daysofMonth domain. For example, on 4: 2, it means the second Wednesday of a month.
Examples of common expressions
0To take 2 *? Indicates that the task is executed every 2 seconds
0 0B 2 *? Indicates that the task is executed every 2 minutes
0 0 21 *? Indicates that the task is adjusted at 2: 00 a. M. on the 1st of each month
0 15 10? * MON-FRI says homework is performed at 10:15 every day from Monday to Friday
0 010, 14, 14, 16? 10:00, 2pm, 4pm every day
0 0x30 9-17 *? Every half hour during working hours from nine to five
0 012? * WED means 12:00 every Wednesday
0 012 *? Triggered at 12:00 every day
0 15 10? * triggered at 10:15 every morning
0 15 10 *? Triggered at 10:15 every morning
0 15 10 *? Triggered at 10:15 every morning
0 * 14 *? Triggered every minute between 2pm and 2:59 every day
0 0B 5 14 * *? Triggered every 5 minutes between 2pm and 2:55 every day
0 010 5 14 14 18 * *? Triggered every 5 minutes between 2pm and 2:55 and between 6pm and 6:55 every day
0 0-5 14 *? Triggered every minute between 2pm and 2:05 every day
0 10 WED 44 14? 3 trigger at 2:10 and 2:44 in the afternoon on Wednesday in March every year
0 15 10? * MON-WED,SAT triggers at 10:15 from Monday to Wednesday and Saturday
0 15 10 15 *? Triggered at 10:15 on the 15th of each month
0 15 10 L *? Triggered at 10:15 on the last day of each month
0 15 10? * 6L triggered at 10:15 on the last Friday of each month
0 15 10? * 6: 3 triggered at 10:15 on the third Friday of each month
@ Scheduled scheduled task summary @ Scheduled
Function: spring timer (execute once regularly or poll regularly to execute a piece of code)
Usage scenarios: annotations on methods
Parameter description: common parameter
@ Scheduled parameter description
The String cron:cron expression defines the time rules for the execution of the method (there is no ink for a lot of instructions on the Internet).
Generator tool address-http://cron.qqe2.com/
Long fixedDelay: how often a scheduled task is executed in milliseconds, and the next execution time is calculated after the end of the last task.
Example: @ Scheduled (fixedDelay = 1000 * 10) / / send once every 10 seconds
Scheduled1 starts to execute 2018-07-27 14:00:00
Scheduled1 ends execution 2018-07-27 14:00:05
Scheduled2 starts to execute 2018-07-27 14:00:15
Scheduled2 ends execution 2018-07-27 14:00:20
Scheduled3 starts to execute 2018-07-27 14:00:30
Scheduled3 ends execution 2018-07-27 14:00:35
Long fixedRate: like fixedDelay, it represents the execution interval of scheduled tasks, except that fixedRate is not affected by the end time of the last task.
Example: @ Scheduled (fixedRate = 1000 * 10) / / send once every 10 seconds
Scheduled1 starts to execute 2018-07-27 14:00:00
Scheduled1 ends execution 2018-07-27 14:00:05
Scheduled2 starts to execute 2018-07-27 14:00:10
Scheduled2 ends execution 2018-07-27 14:00:15
Scheduled3 starts to execute 2018-07-27 14:00:20
Scheduled3 ends execution 2018-07-27 14:00:25
Long initialDelay: the timer is not executed immediately after the project is started, but is delayed according to the value of initialDelay.
Matters needing attention
1. There are two ways to write the parameters of a timer using cron expressions, or using parameters such as fixedDelay and fixedRate to configure them directly.
It should be noted that the method using cron expressions does not run directly after the project is started for the first time, but waits for the execution cycle to execute.
Using the timer method of the second method, the execution will be started immediately after the successful start of the project, and then executed according to the time period.
Test description:
With the first configuration, the method will not be executed after the project is started, but will not be executed until the execution cycle arrives.
Using the second parameter method, the project was executed immediately after it was successfully started.
two。 The timer defaults to single thread, so configure a thread pool if multiple timers are used in the project
Notice the @ EnableScheduling here, and use it to start the timer annotation.
@ Configuration@EnableSchedulingpublic class SchedulingConfig implements SchedulingConfigurer {@ Override public void configureTasks (ScheduledTaskRegistrar taskRegistrar) {taskRegistrar.setScheduler (taskExecutor ());} @ Bean (destroyMethod= "shutdown") public Executor taskExecutor () {return Executors.newScheduledThreadPool (5, new ThreadFactory () {private final AtomicLong counter = new AtomicLong () @ Override public Thread newThread (Runnable r) {Thread thread = newThread (r); thread.setName ("test-scheduler-" + counter.incrementAndGet ()); return thread }});}} at this point, I believe you have a better understanding of "what the @ Scheduled parameter and cron expression are", so you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.