Spring @SCHEDULED (CRON = "0 0 * * *?") Implementing timed Tasks

Source: Internet
Author: User

Spring config file xmlns join

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

Join in Xsi:schemalocation

     Http://www.springframework.org/schema/task     http://www.springframework.org/schema/task/spring-task-3.0.xsd "

Configuration of Spring Scan annotations

<context:component-scan base-package= "com.imwoniu.*"/>

Task Scan Annotations

<task:executor id= "Executor" pool-size= "5"/>  <task:scheduler id= "Scheduler" pool-size= "  />" <task:annotation-driven executor= "Executor" scheduler= "Scheduler"/>

Code implementation:

Note @scheduled can be added as a trigger source to a method, for example, the following method will be executed at a fixed delay time of 5 seconds, which is the completion time of one of the above calling tasks, and after the previous task completes, 5s executes again:

@Scheduled (Fixeddelay = periodically) public void dosomething () {     //something that should execute}

If you need to perform at a fixed rate, as long as you change the name of the property specified in the annotation to Fixedrate, the following method invokes the execution at a fixed rate of 5s, which is the baseline of the above task start time, and is called again after the previous task begins execution 5s:

@Scheduled (fixedrate = periodically) public void dosomething () {     //something that should execute}

If a simple recurring schedule cannot be met, the CRON expression provides the possibility

Package Com.imwoniu.task;import Org.springframework.scheduling.annotation.scheduled;import  org.springframework.stereotype.Component; @Componentpublic class Taskdemo {        @Scheduled (cron = "0 0 2 * *?") Execute        void Dosomethingwith () {            logger.info ("Scheduled task start ...") every day two o'clock in the morning;            Long begin = System.currenttimemillis ();                    Perform database operation Oh ...                    Long end = System.currenttimemillis ();            Logger.info ("Timed task end, total time: [" + (End-begin)/1000 + "] seconds");}    }

  

sequentially seconds (0~59)

Minutes (0~59)

Hours (0~23)

Days (months) (0~31, but you need to consider the number of days of your month)

Month (0~11)

Days (weeks) (1~7 1=sun or Sun,mon,tue,wed,thu,fri,sat)

7. Year (1970-2099)

Spring @SCHEDULED (CRON = "0 0 * * *?") Implementing timed 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.