Spring @Scheduled Keywords

Source: Internet
Author: User

Note: In this example we are using the Spring 4.2.5 version.

to modify a label in the spring context file:

Increase Xmlns tag:xmlns:task=http://www.springframework.org/schema/task

to modify the Xsi:schemalocation label, add the following:

Http://www.springframework.org/schema/task

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

Increase task scanning:

<task:annotation-driven/>

The XML is as follows:

<?xml version= "1.0" encoding= "UTF-8"?>

<beansxmlns= "Http://www.springframework.org/schema/beans"

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

Xmlns:xsi=http://www.w3.org/2001/xmlschema-instancexmlns:jaxws= "Http://cxf.apache.org/jaxws"

xmlns:tx= "Ttp://www.springframework.org/schema/tx"

Xmlns:mvc= "Http://www.springframework.org/schema/mvc"

xmlns:aop= "TTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"

Xmlns:util= "Http://www.springframework.org/schema/util"

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

Xsi:schemalocation= "Http://www.springframework.org/schema/beans

Http://www.springframework.org/schema/beans/spring-beans-4.2.xsd

Http://www.springframework.org/schema/context

Http://www.springframework.org/schema/context/spring-context-4.2.xsd

Http://cxf.apache.org/jaxws

Http://cxf.apache.org/schemas/jaxws.xsd

Http://www.springframework.org/schema/tx

Http://www.springframework.org/schema/tx/spring-tx-4.2.xsd

Http://www.springframework.org/schema/mvc

Http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd

Http://www.springframework.org/schema/util

Http://www.springframework.org/schema/util/spring-util-4.2.xsd

Http://www.springframework.org/schema/aop

Http://www.springframework.org/schema/aop/spring-aop-4.2.xsd

Http://www.springframework.org/schema/task

http://www.springframework.org/schema/task/spring-task-4.2.xsd">

<context:component-scanbase-package= "com"/>

<task:annotation-driven/>

<context:annotation-config/>

</beans>

Implementing class Usage annotations @Component as a comment

Java instance:

@Component

public class Cron {

@Scheduled (cron= "0 0 * * * *?")

Publicvoid Runtaskperhour () {

System.out.println ("0 minutes and 0 seconds per hour :" + new Date ());

}

@Scheduled (Fixedrate= 5000)

Publicvoid Runtaskperfivesecond () {

System.out.println ("Execute now, execute once every 5 seconds:" + new Date ());

}

@Scheduled (fixedrate=, initialdelay = 10000)

Publicvoid Runtaskperfiveseconddelay () {

SYSTEM.OUT.PRINTLN (" delay of ten seconds execution, executed once every 5 seconds:" + new Date ());

}

}

The following reference Iteye

Cron Introduction:

a cron expression has at least 6 time elements separated by a space.

In order of

1. seconds (0~59)

2. minutes (0~59)

3. hours (0~23)

4. Day (month) (0~31, but you need to consider the number of days of your month)

5. month (0~11)

6. Day (week) (1~7 1=sun or SUN,MON,TUE,WED,THU,FRI, SAT)

each of these elements can be a value ( e.g. 6), a continuous interval (9-12), a time interval (8-18/4) (/ = every 4 hours ), a list (1,3,5), a wildcard character. Because the " date in the month " and " date in the week " are mutually exclusive, one of the two elements must be set .

"0 0 10,14,16 * *?" every morning , 2 p.m. ,4 o ' clock.

"0 0/30 9-17 * *?" every half hour for nine to five working hours

"0 0 12?" * WED " means every Wednesday noon .

"0 0 12 * *?" Trigger every day at noon .

"0 15 10?" * * " every morning 10:15 trigger

"0 * 14 * *?" Every 1 minutes at 2 pm to 2:59 pm .

"0 0/5 14 * *?" Every 5 minutes at 2 pm to 2:55 pm .

"0 0/5 14,18 * *?" Every 5 minutes during the afternoon from 2 to 2:55 and 6 pm to 6:55 .

"0 0-5 14 * *?" Every 1 minutes at 2 pm to 2:05 pm .

"0 10,44 14?" 3 WED "2:10 and 2:44 triggered every March Wednesday of the afternoon

"0 15 10?" * Mon-fri " Monday to Friday morning 10:15 trigger

"0 15 10 15 *?" monthly morning 10:15 trigger

"0 L *?" 10:15 Trigger on the last day of the month


Spring @Scheduled Keywords

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.