Spring Boot Configuration Timer task

Source: Internet
Author: User
Tags dateformat

 PackageCom.zooper.demo;ImportJava.text.SimpleDateFormat;Importjava.util.Date;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;Importorg.springframework.scheduling.annotation.Scheduled;Importorg.springframework.stereotype.Component; @Component Public classScheduledtasks {Private Static FinalLogger log = Loggerfactory.getlogger (scheduledtasks.class); Private Static FinalSimpleDateFormat DateFormat =NewSimpleDateFormat ("HH:mm:ss"); /*** Executed every 5 seconds*/@Scheduled (fixedrate= 5000)     Public voidReportcurrenttime () {Log.info ("The time is now {}", Dateformat.format (NewDate ())); }    /*** Trigger timed task According to cron expression format * Cron expression format: * 1.Seconds Minutes Hours dayofmonth Month DayofWeek Year * 2.Seconds Minutes Hours dayofmonth Month DayofWeek * Order: * seconds (0~59) * minutes (0~59) * Hours (0~2      3) * Days (months) (0~31, but you need to consider the number of days of your month) * MONTH (0~11) * Day (week) (1~7 1=sun or Sun,mon,tue,wed,thu,fri,sat) *     Year (1970-2099) * Note: Each element can be a value (such as 6), a continuous interval (9-12), a time interval (8-18/4) (/= every 4 hours), a list (1,3,5), and a wildcard character.     * Because the "date in month" and "Day of the Week" are mutually exclusive, one of the two elements must be set?. */@Scheduled (Cron= "5 * * * *?")     Public voidcronscheduled () {Log.info ("Cron:the time is now {}", Dateformat.format (NewDate ())); }}

Enable Scheduled Tasks
 PackageCom.zooper.demo;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;Importorg.springframework.scheduling.annotation.EnableScheduling, @SpringBootApplication @enablescheduling Public classApplication { Public Static voidMain (string[] args)throwsException {springapplication.run (application.class); }}

Spring Boot Configuration Timer task

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.