Spring boot uses schedule to implement timed tasks

Source: Internet
Author: User

The applicable tools are: Schedule

Integration steps:

1. Open Schedule Support

 PackageCom.jsoft.springboottest.springboottest1;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;Importorg.springframework.scheduling.annotation.EnableScheduling;/*** Hello world! **/@SpringBootApplication@EnableScheduling  Public classApp { Public Static voidMain (string[] args) {Springapplication.run (App.class, args); }}

2. Use

 PackageCom.jsoft.springboottest.springboottest1.controller;Importjava.util.Date;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;Importorg.springframework.scheduling.annotation.Scheduled;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController; @RestController Public classTestController {Private Static FinalLogger Logger = Loggerfactory.getlogger (TestController.class); @Scheduled (fixedrate = () Public voidReportcurrenttime () {Logger.info ("Execute every Five seconds:" +NewDate (). toString ()); } @Scheduled (cron = "15 52 21?" * *")     Public voidfixtimeexecution () {Logger.info ("Execute at specified time" +NewDate (). toString ()); } @RequestMapping ("/show")     PublicString Show () {return"Hello World"; }}

Description: A cron expression that supports Linux.


* First, the second, the value of 0-59* second position, the number of points, the value of 0-59* third, the hour, the value 0-23* fourth, Date day/day, value 1-31* fifth, date and month, value 1-12* sixth bit, week, value 1-7, Monday, Tuesday ..., note: Not 1th week, second week meaning, plus: 1 means Sunday, 2 means Monday. * 7th for, year, can be left blank, value 1970-2099(*asterisk: Can be understood as each meaning, per second, per minute, daily, monthly, every year ... (?question mark: The question mark can only appear in the date and week two positions, indicating that the value of this position is not determined, 3 points per day is executed, so the position of the sixth week, we do not need to pay attention to, that is, indeterminate value. At the same time: the date and week are two mutually exclusive elements, with a question mark indicating that no value is specified. For example, January 10, such as the Week 1, if the location of the week is another designated Tuesday, the conflict conflicts. (-) minus sign: Expresses a range, such as using "10-12" in the hour field, which means from 10 to 12 points, i.e. 10,11,12(,) Comma: Expresses a list value, as used in the day of the week field "1,2,4", it means Monday, Tuesday, Thursday (/) Slash: such as: X/y,x is the start value, Y is the step, such as in the first bit (seconds) 0/15 is, starting from 0 seconds, every 15 seconds, and finally is 0,15,30,45,60 another: */y, equivalent to 0/Y Example:0 0 3 * *?3-point execution per day0 5 3 * *?performed 3:5 daily0 5 3? * *executes 3:5 daily, same as above0 5/10 3 * *?3 points per day, 5 points, 15 points, 25 points, 35 points, 45 points, 55 points these hours are executed.0 10 3? * 1Sunday, 3:10 per week, note: 1 means Sunday0 10 3? * 1#3 The third week of each month, executed in Sunday, #号只能出现在星期的位置

Example Project: Https://github.com/easonjim/5_java_example/tree/master/springboottest/springboottest8


Http://www.importnew.com/27287.html (the above content is transferred from this article)


Spring boot uses schedule to implement 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.