The SPRINGMVC feature is very powerful and integrates the functions of the quartz timer, which allows you to perform scheduled tasks with cron expressions and simple annotations.
There are many examples on the internet, but they are not very whole.
To talk less, first add the following lines to the Springmvc.xml:
xmlns:task= "Http://www.springframework.org/schema/task"
<!--The following two lines are to be placed in xsi:schemalocation--
Http://www.springframework.org/schema/task
Http://www.springframework.org/schema/task/spring-task-3.2.xsd
With these two lines of code, you can add the XML code for the timer configuration in the configuration file. Examples are as follows:
Or in the springmvc.xml, these two lines no longer need to explain, let SPRINGMVC know where to scan the annotated file:
<!--annotation Scan Package--
<context:component-scan base-package= "Com.cmsv2.controller"/>
<!--opening annotations--
<mvc:annotation-driven/>
Then add the following:
<!--timer configuration
task:executor/@pool-size: You can specify the initial size, maximum size of the execution thread pool
task:executor/@queue-capacity: Capacity of the task queue waiting to be executed
task:executor/@rejection-policy: When waiting for the team to be full, the strategy is divided into discarded, run directly by the task executor, etc.
-
<task:scheduler id= "Scheduler" pool-size= "ten"/>
<task:executor id= "executor" keep-alive= "3600 " pool-size= "100-200"
queue-capacity= "rejection-policy=" Caller_runs "/>
<task:annotation-driven executor= "executor" scheduler= "Scheduler"/>
These lines copy from the Web.
Also add a aopaliaance.jar, or you will get an error: Noclassdefounderror:org/aopalliance/aop/advice
Address: http://mirrors.ibiblio.org/pub/mirrors/maven2/aopalliance/aopalliance/1.0/
After download add to BuildPath.
This completes the configuration work.
Start writing code below:
Import Java.util.date;import Org.springframework.scheduling.annotation.scheduled;import org.springframework.stereotype.Component; @Component public class ScheduledTest2 {@Scheduled (cron = "0 0/1 * * *?") public void Runfunction () {System.out.println (new Date () + "Package.controller scheduled test--and Mahaha");}}
And then it's OK! Execute once per minute ~ ~ ~
Reference: http://bbs.csdn.net/topics/260068512
Http://www.2cto.com/kf/201311/257405.html
http://blog.csdn.net/xiao_wgs69/article/details/11269391
SPRINGMVC Timer with cron expression