SPRINGMVC Timer with cron expression

Source: Internet
Author: User

The SPRINGMVC feature is powerful and integrates the functions of the quartz timer. The ability to perform scheduled tasks with cron expressions and simple annotations.


See a lot of examples on the Internet, but are not very full.


The gossip is seldom said. 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 of the timer configuration to the configuration file. Examples include the following:


Or in the springmvc.xml, these two lines need not be explained. Let Springmvc know where to scan the annotated file:

<!--annotation Scan Package--
<context:component-scan base-package= "Com.cmsv2.controller"/>

<!--a second note package. There's only @scheduled, so don't scan the controller---
<context:component-scan base-package= "Com.cmsv2.schedule" >
<context:exclude-filter type= "annotation" expression= "Org.springframework.stereotype.Controller"/ >
</context:component-scan>

<!--opening annotations--
<mvc:annotation-driven/>



Then add the following:

<!--timer configuration
task:executor/@pool-size: Ability to specify the initial size, maximum size of the running thread pool
task:executor/@queue-capacity: Capacity of the task queue waiting to run
task:executor/@rejection-policy: The policy when waiting for the team to be full. Divided into discarded, executed 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 at the same time, 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.


Here's how to start writing code:


<span style= "font-family:arial, Helvetica, Sans-serif;" >import java.util.date;</span>
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! Run once per minute ~ ~ ~


References: 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.