SPRINGMVC Timer with cron expression

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.