Configuring timed tasks in Springboot

Source: Internet
Author: User

Objective

Prior to using timed tasks in spring, the use of annotations is easy to configure, the configuration in Springboot is basically the same, but some of the original XML files in spring configuration needs to change, in the Springboot is also very simple.

Already added to my GitHub template: https://github.com/LinkinStars/springBootTemplate

Classification of Scheduled Tasks

The so-called timed task is to perform a task periodically after the project is launched to meet the needs of the business.

Scheduled tasks are divided into the following types, serial, parallel, synchronous, asynchronous

Serial, Parallel: When more than one scheduled task is configured, the serial method will only have one thread to perform all the tasks, and the previous task executes, and the latter task will start executing, while parallelism means that multiple threads are started to perform all tasks, and the tasks are executed at the same time.

Synchronous, asynchronous: This is for a timed task, synchronization means that the scheduled task itself is completed before the execution of their own, asynchronous refers to the task itself, even if not completed, when the timing of the arrival will be executed again

For example (timed tasks are set to execute every two seconds, but sleep is 3 seconds in execution)

[2018-02-08 09:45:14.005] [Pool-1-thread-1] [INFO] My_info-Task 1: Execute
[2018-02-08 09:45:17.007] [Pool-1-thread-1] [INFO] My_info-Task 1: Execution complete
[2018-02-08 09:45:17.009] [Pool-1-thread-1] [INFO] My_info-Task 2: Execute
[2018-02-08 09:45:20.012] [Pool-1-thread-1] [INFO] My_info-Task 2: Execution complete
[2018-02-08 09:45:20.013] [Pool-1-thread-1] [INFO] My_info-Task 3: Execute
[2018-02-08 09:45:23.016] [Pool-1-thread-1] [INFO] My_info-Task 3: Execution complete
[2018-02-08 09:45:23.017] [Pool-1-thread-1] [INFO] My_info-Task 4: Execute
[2018-02-08 09:45:26.021] [Pool-1-thread-1] [INFO] My_info-Task 4: Execution complete
[2018-02-08 09:45:26.022] [Pool-1-thread-1] [INFO] My_info-Task 1: Execute
[2018-02-08 09:45:29.026] [Pool-1-thread-1] [INFO] My_info-Task 1: Execution complete

You can see that these four tasks are performed by the same thread, and the next task will start only when the current one is complete, so it is currently serial synchronized

[2018-02-08 10:02:42.004] [Pool-1-thread-1] [INFO] My_info-Task 3: Execute
[2018-02-08 10:02:42.004] [Pool-1-thread-3] [INFO] My_info-Task 4: Execute
[2018-02-08 10:02:42.004] [Pool-1-thread-4] [INFO] My_info-Task 1: Execute
[2018-02-08 10:02:42.004] [Pool-1-thread-2] [INFO] My_info-Task 2: Execute
[2018-02-08 10:02:45.011] [Pool-1-thread-4] [INFO] My_info-Task 1: Execution complete
[2018-02-08 10:02:45.011] [Pool-1-thread-2] [INFO] My_info-Task 2: Execution complete
[2018-02-08 10:02:45.011] [Pool-1-thread-3] [INFO] My_info-Task 4: Execution complete
[2018-02-08 10:02:45.011] [Pool-1-thread-1] [INFO] My_info-Task 3: Execution complete
[2018-02-08 10:02:46.005] [Pool-1-thread-2] [INFO] My_info-Task 2: Execute
[2018-02-08 10:02:46.005] [Pool-1-thread-4] [INFO] My_info-Task 1: Execute
[2018-02-08 10:02:46.005] [Pool-1-thread-5] [INFO] My_info-Task 4: Execute
[2018-02-08 10:02:46.005] [Pool-1-thread-8] [INFO] My_info-Task 3: Execute
[2018-02-08 10:02:49.011] [Pool-1-thread-2] [INFO] My_info-Task 2: Execution complete
[2018-02-08 10:02:49.011] [Pool-1-thread-4] [INFO] My_info-Task 1: Execution complete
[2018-02-08 10:02:49.011] [Pool-1-thread-8] [INFO] My_info-Task 3: Execution complete
[2018-02-08 10:02:49.011] [Pool-1-thread-5] [INFO] My_info-Task 4: Execution complete

You can see that these four tasks are performed by different threads and are currently performing the next task only if the previous task is completed, so it is currently in parallel synchronization

The second way we like to use it in our projects

How to configure

If you use the serial method as follows

Importorg.springframework.context.annotation.Configuration;Importorg.springframework.scheduling.annotation.EnableScheduling;Importorg.springframework.scheduling.annotation.Scheduled;/*** Scheduled Task configuration *@authorLinkinstar*/@Configuration @enablescheduling Public classtimetaskconfig {@Scheduled (cron= "0/5 * * * *?")     Public voidtest1 () {System.out.println ("Task 1: Execute"); }}

If you use parallel methods as follows

Importorg.springframework.context.annotation.Configuration;Importorg.springframework.scheduling.annotation.EnableScheduling;ImportOrg.springframework.scheduling.annotation.SchedulingConfigurer;ImportOrg.springframework.scheduling.config.ScheduledTaskRegistrar;Importjava.util.concurrent.Executors;/*** Scheduled Task configuration *@authorLinkinstar*/@Configuration @enablescheduling Public classTimetaskconfigImplementsSchedulingconfigurer {/*** Configure the Timer task thread pool size*/@Override Public voidconfiguretasks (Scheduledtaskregistrar taskregistrar) {Taskregistrar.setscheduler (Executors.newScheduledThrea Dpool (10)); }}
ImportCom.linkinstars.springBootTemplate.util.LogUtil;Importorg.springframework.context.annotation.Configuration;Importorg.springframework.scheduling.annotation.Scheduled;/*** Scheduled Task configuration *@authorLinkinstar*/@Configuration Public classtimetask {@Scheduled (cron= "0/2 * * * *?")     Public voidtest1 () {Logutil.printlog ("Task 1: Execute"); Try{Thread.Sleep (3000); } Catch(interruptedexception e) {e.printstacktrace (); } logutil.printlog ("Task 1: Execution Complete"); }}

Reference

https://www.jianshu.com/p/ef18af5a9c1d

Https://www.cnblogs.com/slimer/p/6222485.html

Configuring timed tasks in Springboot

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.