Enterprise Distribution Micro Service Cloud Springcloud springboot MyBatis (32) Spring boot do the dispatch task

Source: Internet
Author: User
Tags dateformat

Building projects

Create a Springboot project, add @enablescheduling to its program entry, and start the dispatch task.

@SpringBootApplication @enableschedulingpublic class Springbootschedulingtasksapplication {public    static void Main (string[] args) {        springapplication.run (springbootschedulingtasksapplication.class, args);}    }

  

Create a timed task

Create a timed task that prints the current time in the console every 5s.

@Componentpublic class Scheduledtasks {    private static final Logger log = Loggerfactory.getlogger ( Scheduledtasks.class);    private static final SimpleDateFormat DateFormat = new SimpleDateFormat ("HH:mm:ss");    @Scheduled (fixedrate =) public    void Reportcurrenttime () {        log.info ("The time was now {}", Dateformat.format ( New Date ()));}    }

  

By adding @scheduled annotations to the method, it shows that the method is a dispatch task.

    • @Scheduled (fixedrate = 5000): Executes 5 seconds after the last start point of execution
    • @Scheduled (Fixeddelay = 5000): Executed 5 seconds after the last execution time
    • @Scheduled (initialdelay=1000, fixedrate=5000): Executes after the first delay of 1 seconds, followed by fixedrate rules every 5 seconds
    • @Scheduled (cron= "/5"): Define rules by cron expressions, what are CRO expressions, self-search engines.
Test

Start the Springboot project, the console has not been 5s to print out the current time.

2017-04-29 17:39:37.672 INFO 677-[pool-1-thread-1] com.forezp.task.ScheduledTasks:The time is now 17:39:37
2017-04-29 17:39:42.671 INFO 677-[pool-1-thread-1] com.forezp.task.ScheduledTasks:The time is now 17:39:42
2017-04-29 17:39:47.672 INFO 677-[pool-1-thread-1] com.forezp.task.ScheduledTasks:The time is now 17:39:47
2017-04-29 17:39:52.675 INFO 677-[pool-1-thread-1] com.forezp.task.ScheduledTasks:The time is now 17:39:52

Summarize

Creating a timed task in Springboot is simple and takes only 2 steps:

    • 1. Add @enablescheduling annotations at the entrance of the program.
    • 2. Add @scheduled annotations to the timing method.

Source Source

Enterprise Distribution Micro Service Cloud Springcloud springboot MyBatis (32) Spring boot do the dispatch 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.