Project development needs to perform some timing tasks, such as the need to analyze the log information of the day before the early hours of the morning. Usually use quartz more, but configuration relatively troublesome point. Today, we'll talk about Spring's own scheduled tasks.
Spring comes with timing tasks in two ways, one through annotations, and one by configuration in a configuration file.
First, the timing task is realized by spring annotations (@Scheduled).
First of all, of course, Springde configuration:
The first step: add these three paragraphs:
xmlns:task= " Http://www.springframework.org/schema/task "
Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7E/C8/wKioL1cIwsTBnQhvAADUnngjwjg151.png "title=" QQ picture 20160409164902.png "alt=" Wkiol1ciwstbnqhvaadunngjwjg151.png "/>
Step two: Turn on the spring timer annotation switch
<task:annotation-driven/>
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7E/C8/wKioL1cIw0mBMIrtAAAXH-uwSqQ355.png "title=" QQ picture 20160409165121.png "alt=" Wkiol1ciw0mbmirtaaaxh-uwsqq355.png "/>
Step three: Write a timed task class and its methods, and add annotations for classes and methods, which can have multiple methods.
import java.text.simpledateformat;import java.util.date;import org.springframework.context.annotation.Lazy;import org.springframework.scheduling.annotation.scheduled;import org.springframework.stereotype.component;@ Component ("Scheduledmanager") @Lazy (Value=false) public class scheduledmanager {public Static simpledateformat simpledateformat = new simpledateformat ("Yyyy-MM-dd HH: Mm:ss ");//Task One @scheduled (cron = " 0/5&NBSP;*&NBSP;*&NBSP;*&NBSP;*&NBSP;? ") Public void autocardcalculate () {system.out.println (Simpledateformat.format (New Date ()) + "&NBSP;: performing in Task 1 ..."); Task two @scheduled (cron = "0/5&NBSP;*&NBSP;*&NBSP;*&NBSP;*&NBSP;?") Public void autocardcalculate2 () {system.out.println (Simpledateformat.format (New Date ()) + "&NBSP;: Executing in Task 2 ...");}
Fourth step: Start the service, the effect is immediate
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7E/CC/wKiom1cIxFGxoFDLAABTtYLD3aI918.png "title=" 6666. PNG "alt=" Wkiom1cixfgxofdlaabttyld3ai918.png "/>
+++++++++++++++++++++++++++ above is to realize the +++++++++++++++++++++++++ of the timed task by the way of annotations
++++++++++++++++ The following is the implementation of timed tasks by configuring them in the Spring configuration file +++++++++++++++++++++
Second, implement timed tasks with spring's configuration file.
Unlike annotations, timing tasks are controlled in the configuration file rather than in annotations.
That is, the following paragraph is configured in the spring configuration file, and you do not need to @Scheduled this annotation in the code.
<task:scheduled-tasks scheduler= "Myscheduler" > <task:scheduled ref= "Scheduledmanager" method= "AutoCardCa Lculate "cron=" 0/5 * * * * * "/> <task:scheduled ref=" Scheduledmanager "method=" AutoCardCalculate2 "cron=" 1/ 5 * * * * * "/> </task:scheduled-tasks> <task:scheduler id=" Myscheduler "pool-size="/>
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7E/CC/wKiom1cIxX2yPngIAABlbbhKIZ0926.png "title=" 6.png " alt= "Wkiom1cixx2ypngiaablbbhkiz0926.png"/>
The only thing to note is the @lazy (Vlaue=false) in the timed task class, which is a lazy-loaded annotation that can be left out, but if you change the value to True, the scheduled task will not execute. Since the spring container is initialized, it does not trigger the timed task when it is added.
This article is from the "Simple Life" blog, so be sure to keep this source http://simplelife.blog.51cto.com/9954761/1762108
Using the spring self-timer: @Scheduled