Note @scheduled can be added as a trigger source to a method, for example, the following method will be executed at a fixed delay time of 5 seconds, which is the completion time of one of the above calling tasks, and after the previous task completes, 5s executes again:
1 @Scheduled (fixeddelay=5000)2publicvoid dosomething () { 3 // something that should execute periodically 4 }
If you need to perform at a fixed rate, as long as you change the name of the property specified in the annotation to Fixedrate, the following method invokes the execution at a fixed rate of 5s, which is the baseline of the above task start time, and is called again after the previous task begins execution 5s:
1 @Scheduled (fixedrate=5000)2publicvoid dosomething () { 3 // something that should execute periodically4 }
For fixed-delay and fixed-rate tasks, you can specify an initial delay that represents the number of milliseconds that the method waits before the first call is executed:
1 @Scheduled (initialdelay=1000, fixedrate=5000)2publicvoid DoSomething () {3 // something that should execute periodically4 }
If a simple recurring schedule cannot be met, then the cron expression provides the possibility. For example, the following method will only be performed on weekdays:
1 @Scheduled (cron= "*/5 * * * * * MON-FRI")2publicvoid dosomething () { 3 // something that should execute on weekdaysonly4 }
You can also specify the time zone that a cron expression is called by using the Zone property.
Attention:
1, spring annotation @scheduled need to write on the implementation method;
2, the task method of the timer can not have a return value (if there is a return value, spring initialization will tell you that there is an error, you need to set a Proxytargetclass value of true), can not point to any parameters;
3. If the method needs to interact with other objects in the application context, it is usually done through dependency injection;
4, the realization class to have the component annotation @component.
Reference Document:http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/# Scheduling-annotation-support-scheduled
Use annotations @Scheduled to perform timed tasks in spring