Spring comes with a timed task function @enablescheduling

Source: Internet
Author: User

1 Demo

package Com.test.domi.config;import Org.springframework.beans.factory.annotation.configurable;import Org.springframework.scheduling.annotation.enablescheduling;import Org.springframework.scheduling.annotation.scheduled;import Org.springframework.stereotype.component;import Java.text.simpledateformat;import java.util.Date; @Component @configurable@enableschedulingpublic class Scheduledtasks {//every 30 seconds @Scheduled (fixedrate = $ *) public void Reportcurrenttime () {System.out.    println ("Scheduling Tasks examples:the time is now" + dateformat (). Format (new Date ())); }//execute @Scheduled at fixed time (cron = "0 */1 * * * *") public void Reportcurrentbycron () {System.out.println ("S    Cheduling Tasks Examples by cron:the time was now "+ dateformat (). Format (new Date ()));    } private SimpleDateFormat DateFormat () {return new SimpleDateFormat ("HH:mm:ss"); }}
Scheduling Tasks examples:the time is now 11:55:5411:56:0011:56:2411:56:54  11:57:00

2 explanation

http://tramp.cincout.cn/2017/08/18/spring-task-2017-08-18-spring-boot-enablescheduling-analysis/

Cron expression: HTTPS://WWW.ZHYD.ME/ARTICLE/43

3 Summary

The default Concurrenttaskscheduler plan executor implements a single-threaded actuator with executors.newsinglethreadscheduledexecutor (). Therefore, execution of the same dispatch task is always the same thread. If the execution time of the task exceeds the next execution time of the task, a task is lost and the task is skipped for that period of time. The above problems have the following solutions:

Executes the dispatch task asynchronously, configures the Spring @EnableAsync, and marks the @Async configuration task execution pool on the method of the task execution, using Threadpooltaskscheduler.setpoolsize (n). The number of n is the time required for a single task to execute/task execution interval

 

Spring comes with a timed task function @enablescheduling

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.