Perform timed tasks using spring @Scheduled annotations

Source: Internet
Author: User

The previous framework used the quartz framework to perform timing scheduling problems,

The boss said the configuration is too cumbersome, each schedule needs to be added in the spring configuration,

Can reduce the amount of configuration to improve development efficiency,

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

Feel very convenient, at least the configuration of things a lot less,

So stay for forgetting,

First, we need to configure our Spring.xml

xmlns add the following content,

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

Then xsi:schemalocation add the following content,

Java code
    1. http://www.springframework.org/schema/task
    2. http://www.springframework.org/schema/task/spring-task-3.1.xsd

And finally, our task mission scan annotations.

Java code
    1. <task:annotation-driven/>

My configuration scan location is:

Java code
    1. <context:annotation-config/>
    2. <bean class="Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
    3. <context:component-scan base-package="com.test"/>

Scan the contents of a package such as Com.test,

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

[Java]View Plaincopy
    1. Public interface Imytestservice {
    2. public void MyTest ();
    3. }



[Java]View Plaincopy
  1. @Component //import org.springframework.stereotype.Component;
  2. Public class Mytestserviceimpl implements Imytestservice {
  3. @Scheduled (cron="0/5 * * * *?") //Once every 5 seconds
  4. @Override
  5. public void MyTest () {
  6. System.out.println ("Enter Test");
  7. }
  8. }

After execution, the console will print out and enter the test.

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,

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

Perform scheduled tasks using spring @Scheduled annotations

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.