Create a scheduled task in Spring3 using annotations (@Scheduled)

Source: Internet
Author: User

The use of annotations is enhanced in Spring3, where the scheduled tasks are enhanced, and it is now only two steps to create a scheduled task:

    1. Create a Java class, add a method with no parameter and no return value, and decorate it with @scheduled annotations on the method;
    2. Add three <task:****/> nodes in the spring configuration file;

Finally, the Java class created in the first step is to be a spring-manageable bean that can be written directly in XML or @component.

To schedule a task class:

/*** Com.zywang.spring.task.SpringTaskDemo.java *@authorZywang 2011-3-9*/ PackageCom.zywang.spring.task;Importorg.springframework.scheduling.annotation.Scheduled;Importorg.springframework.stereotype.Component;/*** Spring3 @Scheduled Demo *@authorZywang 2011-3-9spring configuration file:*/@Component Public classSpringtaskdemo {@Scheduled (Fixeddelay= 5000)    voidDosomethingwithdelay () {System.out.println ("I ' m doing with delay now!"); } @Scheduled (Fixedrate= 5000)    voiddosomethingwithrate () {System.out.println ("I ' m doing with rate now!"); } @Scheduled (Cron= "0/5 * * * * *")    voidDosomethingwith () {System.out.println ("I ' m doing with cron now!"); }}

Spring configuration file:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:task= "Http://www.springframework.org/schema/task"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd ">    <!--enables the Spring Task @Scheduled programming Model -    <Task:executorID= "Executor"pool-size= "5" />    <Task:schedulerID= "Scheduler"pool-size= "Ten" />    <Task:annotation-drivenExecutor= "Executor"Scheduler= "Scheduler" /></Beans>

A few things to note:

1, spring @scheduled annotations need to write on the implementation,

2, the task method of the timer can not have a return value (if there is a return value, spring initialization will tell you that there is an error, you need to set a Proxytargetclass value of true, go to Baidu Google bar)

3, the implementation of the class to have component annotations @component

The rest is corn expression, specific use and parameters please Baidu Google,

"Seconds" "Minute" "Time" "Day" "Month" "Week" "Year"

Here are just a few examples.

Cron expression meaning
"0 0 12 * *?" Triggered 12 o'clock noon every day
"0 15 10?" * * "trigger 10:15 every day"
"0 15 10 * *?" Triggered 10:15 daily
"0 15 10 * *?" * "10:15 per day" trigger
"0 15 10 * *?" 2005 "2005-year daily 10:15 Trigger
"0 * 14 * *?" Daily from 2 o'clock in the afternoon to 2:59 per minute trigger
"0 0/5 14 * *?" Every 5 minutes from 2 o'clock in the afternoon to 2:55
"0 0/5 14,18 * *?" Daily from 2 o'clock in the afternoon to 2:55 and from 6 to 6:55 every 5 minutes for two time periods
"0 0-5 14 * *?" Daily 14:00 to 14:05 triggers per minute
"0 10,44 14?" 3 WED "March of 14:10 and 14:44 triggers per Wednesday
"0 15 10?" * MON-FRI "10:15 triggers per Monday, Tuesday, Wednesday, Thursday, Friday

Configurable property entries for the scheduled class:

String cronA cron-like expression, extending the usual un*x definition to include triggers on the second as well as minute, hour, day Of month, month and day of week.
long fixedDelayExecute The annotated method with a fixed period between the end of the last invocation and the start of the next.
String fixedDelayStringExecute The annotated method with a fixed period between the end of the last invocation and the start of the next.
long fixedRateExecute The annotated method with a fixed period between invocations.
String fixedRateStringExecute The annotated method with a fixed period between invocations.
long initialDelayNumber of milliseconds to delay before the first execution of a fixedRate() or fixedDelay() task.
String initialDelayStringNumber of milliseconds to delay before the first execution of a fixedRate() or fixedDelay() task.
String zoneA time zone for which the cron expression would be resolved.

The above is based on the Spring 3.0.5 version and the reference document is Spring-framework-reference-3.0.5.pdf

Create a scheduled task in Spring3 using annotations (@Scheduled)

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.