Use spring @Scheduled annotations to run scheduled tasks,

Source: Internet
Author: User

Once the framework uses the Quartz framework to run timing scheduling problems,

The boss said that the configuration is too cumbersome, each scheduling needs to be more in the spring configuration,

Can reduce the amount of configuration to improve development efficiency,

Recently, we have looked at spring's scheduled using annotations to dispatch,

Feel very convenient, at least the configuration of things less very much,


So stay for forgetting,


First, we need to configure our Spring.xml


xmlns add the following content,

xmlns:task= "Http://www.springframework.org/schema/task"


Then xsi:schemalocation add the following content,

Http://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.1.xsd

And finally, our task mission scan annotations.

<task:annotation-driven/>

My configuration scan location is:

<context:annotation-config/>    <bean class= " Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/>    <context: Component-scan base-package= "Com.test"/>


Scanning is the content of this package under Com.test,

The following interfaces and implementations are required (all of my Java files are under the Com.test package)

Public interface Imytestservice {public       void MyTest ();}



@Component  //import Org.springframework.stereotype.component;public class Mytestserviceimpl  implements Imytestservice {      @Scheduled (cron= "0/5 * * * *  ?) ")   //Run once every 5 seconds      @Override public      void MyTest () {            System.out.println (" Enter Test ");}      }

After running the console will print out into the test


Points to note:

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

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

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


The rest is corn expression, detailed use and participation please Baidu Google,

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 * *?" Starts every day from 2 o'clock in the afternoon to 2:55 minutes and ends every 5 minutes.
"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



Use spring @Scheduled annotations to run scheduled tasks,

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.