Spring config file xmlns join
xmlns:task= "Http://www.springframework.org/schema/task"
Join in Xsi:schemalocation
Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd "
Configuration of Spring Scan annotations
<context:component-scan base-package= "com.imwoniu.*"/>
Task Scan Annotations
<task:executor id= "Executor" pool-size= "5"/> <task:scheduler id= "Scheduler" pool-size= " />" <task:annotation-driven executor= "Executor" scheduler= "Scheduler"/>
Code implementation:
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:
@Scheduled (Fixeddelay = periodically) public void dosomething () { //something that should execute}
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:
@Scheduled (fixedrate = periodically) public void dosomething () { //something that should execute}
If a simple recurring schedule cannot be met, the CRON expression provides the possibility
Package Com.imwoniu.task;import Org.springframework.scheduling.annotation.scheduled;import org.springframework.stereotype.Component; @Componentpublic class Taskdemo { @Scheduled (cron = "0 0 2 * *?") Execute void Dosomethingwith () { logger.info ("Scheduled task start ...") every day two o'clock in the morning; Long begin = System.currenttimemillis (); Perform database operation Oh ... Long end = System.currenttimemillis (); Logger.info ("Timed task end, total time: [" + (End-begin)/1000 + "] seconds");} }
sequentially seconds (0~59)
Minutes (0~59)
Hours (0~23)
Days (months) (0~31, but you need to consider the number of days of your month)
Month (0~11)
Days (weeks) (1~7 1=sun or Sun,mon,tue,wed,thu,fri,sat)
7. Year (1970-2099)
Spring @SCHEDULED (CRON = "0 0 * * *?") Implementing timed Tasks