Spring timed Task (1): Implement static timed tasks with component annotations

Source: Internet
Author: User
Tags dateformat

Environment: Myeclipse10.7+spring 3.1

First, write the task class on the server side

Package Com.conbao.component.task.controller;import Java.text.dateformat;import Java.text.simpledateformat;import Java.util.date;import Org.springframework.scheduling.annotation.scheduled;import org.springframework.stereotype.component;/** * Timed Task Job Class * *  @author Code Chen *  /@Component ("task") public class Task {dateformat format = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); @Scheduled (cron = "0 10-30 9 * *?") public void Pushquestionnaire () {System.out.println ("scheduled Task 1, Auto execute:" + format.format (new Date ()));} @Scheduled (cron = "0 10-30 9 * *?") public void PushQuestionnaire2 () {System.out.println ("scheduled Task 2, Auto execute:" + format.format (new Date ()));}}


Second, configure XML on the server side
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "Http://www.springframework.org/schema/aop" xmlns: context= "Http://www.springframework.org/schema/context" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:task= "Http://www.springframework.org/schema/task" xsi:schemalocation= "http://www.springframework.org/ Schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/ SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/ Context Http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsdhttp://www.springframework.org/schema/tx Http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/task/HTTP Www.springframework.org/schema/task/spring-task-3.0.xsd "default-lazy-init=" false "><context: Annotation-config/><!-spring ScanAnnotation configuration: Scan to the package where the task is located, because there are other annotations in my program, so the Base-package is the--><context:component-scan base-package= " Com.conbao.component "/><!-Open this configuration--><task:annotation-driven scheduler=" Qbscheduler "mode=" proxy "/> <task:scheduler id= "Qbscheduler" pool-size= "ten"/></beans>

Third, the final operation effect


< interpretation >

(1) This is a timed task implemented through spring's @component and @scheduled, which can be called a static scheduled task due to the fact that the execution time of the timed task has been written in the code (cron= "* * * * * *"), but in many cases We are not sure of a task execution time, such as in the foreground through a calendar, select the time to perform a task, then we need to dynamically change cron, this is the dynamic timing task, the implementation, the next section and then tidy.

(2) In fact, can also be through the other spring configuration or other means such as Java comes with the timer class or Quartz to achieve scheduled tasks, we can go online to view, I am because the framework of the project is spring, so the use of annotations is more convenient.

(3) Cron set rules, from the Internet pick, can be designed according to their own needs.

Special characters allowed for field allowed values

seconds 0-59,-*/

Sub 0-59,-*/

hours 0-23,-*/

Date 1-31,-*? /L W C

month 1-12 or JAN-DEC,-*/

Week 1-7 or Sun-sat,-*? /L C #

year (optional) leave blank, 1970-2099,-*/

-Interval

* Wildcard Characters

? You don't want to set that field.


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



Spring timed Task (1): Implement static timed tasks with component 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.