Springboot implementation code for dynamic management of timed tasks

Source: Internet
Author: User
This article gives you the content is about the springboot dynamic management of the implementation of the task of the Code, there is a certain reference value, the need for a friend can refer to, I hope you have some help.

Springboot dynamic management of Scheduled tasks

Start a scheduled task using @enablescheduling first

Package Com.fighting;import Org.springframework.boot.springapplication;import Org.springframework.boot.autoconfigure.springbootapplication;import Org.springframework.scheduling.annotation.EnableScheduling, @SpringBootApplication @enableschedulingpublic class scheduledapplication {public    static void Main (string[] args) {        Springapplication.run ( Scheduledapplication.class, args);}    }

Configuring Logging Levels

Logging.level.com= Debuglogging.file=springboot-scheduled.log

Fixed-cycle timing tasks

Package Com.fighting;import Org.slf4j.logger;import Org.slf4j.loggerfactory;import Org.springframework.scheduling.annotation.scheduled;import org.springframework.stereotype.component;/** * Spring Static Cycle Timer Task * * @author Fighting * @date 2018-09-11 */@Componentpublic class Springstaticcrontask {public    static F inal Logger Logger = Loggerfactory.getlogger (springstaticcrontask.class);    @Scheduled (cron = "0/5 * * * * *?")    public void Staticcorntask () {        logger.debug ("Staticcrontask is running ...");}    }

Dynamic modification of timing cycles

Implementing the Schedulingconfigurer interface, overriding the Configuretasks method

Package Com.fighting;import Org.slf4j.logger;import Org.slf4j.loggerfactory;import Org.springframework.scheduling.annotation.schedulingconfigurer;import Org.springframework.scheduling.config.scheduledtaskregistrar;import Org.springframework.scheduling.support.crontrigger;import org.springframework.stereotype.component;/** * Dynamic timed task * * @author Fighting * @date 2018-09-11 */@Componentpublic class Springdynamiccrontask implements Schedulingconfigurer {PR    Ivate static final Logger Logger = Loggerfactory.getlogger (Springdynamiccrontask.class);    private static String cron = "0/5 * * * *?"; @Override public void Configuretasks (Scheduledtaskregistrar scheduledtaskregistrar) {Scheduledtaskregistrar.add        Triggertask ((), {//Task logic Logger.error ("Dynamiccrontask is running ..."); }, Triggercontext, {///task trigger, where you can modify the execution cycle of a task, because each schedule will be executed here Crontrigger Crontrigger = new Crontrigg            ER (cron); Return CRONTRIGGER.NEXTEXECUTiontime (Triggercontext);    });                } public Springdynamiccrontask () {///simulate the business modification cycle, you can modify the parameters in the specific business cron New Thread ((), {try {            Thread.Sleep (15000);            } catch (Interruptedexception e) {e.printstacktrace ();        } cron = "0/2 * * * * *?";    }). Start (); }}

The

observes the print result, the period changes, but the item does not restart.

2018-09-11 13:36:50.009 DEBUG 16708---[pool-1-thread-1] com.fighting.SpringStaticCronTask:staticCronTask is run Ning ... 2018-09-11 13:36:50.010 ERROR 16708---[pool-1-thread-1] com.fighting.SpringDynamicCronTask:dynamicCronTask is RU Nning ... 2018-09-11 13:36:55.001 DEBUG 16708---[pool-1-thread-1] com.fighting.SpringStaticCronTask:staticCronTask is run Ning ... 2018-09-11 13:36:55.002 ERROR 16708---[pool-1-thread-1] com.fighting.SpringDynamicCronTask:dynamicCronTask is RU Nning ... 2018-09-11 13:37:00.009 DEBUG 16708---[pool-1-thread-1] com.fighting.SpringStaticCronTask:staticCronTask is run Ning ... 2018-09-11 13:37:00.016 ERROR 16708---[pool-1-thread-1] com.fighting.SpringDynamicCronTask:dynamicCronTask is RU Nning ... 2018-09-11 13:37:05.016 DEBUG 16708---[pool-1-thread-1] com.fighting.SpringStaticCronTask:staticCronTask is run Ning ...  2018-09-11 13:37:05.016 ERROR 16708---[pool-1-thread-1] com.fighting.SpringDynamicCronTask     : Dynamiccrontask is running ... 2018-09-11 13:37:06.013 ERROR 16708---[pool-1-thread-1] com.fighting.SpringDynamicCronTask:dynamicCronTask is RU Nning ... 2018-09-11 13:37:08.008 ERROR 16708---[pool-1-thread-1] com.fighting.SpringDynamicCronTask:dynamicCronTask is RU Nning ... 2018-09-11 13:37:10.002 ERROR 16708---[pool-1-thread-1] com.fighting.SpringDynamicCronTask:dynamicCronTask is RU Nning ... 2018-09-11 13:37:10.003 DEBUG 16708---[pool-1-thread-1] com.fighting.SpringStaticCronTask:staticCronTask is run Ning ... 2018-09-11 13:37:12.002 ERROR 16708---[pool-1-thread-1] com.fighting.SpringDynamicCronTask:dynamicCronTask is RU Nning ... 2018-09-11 13:37:14.006 ERROR 16708---[pool-1-thread-1] com.fighting.SpringDynamicCronTask:dynamicCronTask is RU Nning ... 2018-09-11 13:37:15.015 DEBUG 16708---[pool-1-thread-1] com.fighting.SpringStaticCronTask:staticCronTask is run Ning ... 2018-09-11 13:37:16.012 ERROR 16708---[pool-1-thread-1] Com.fighting.SpringDynamicCronTask:dynamicCronTask is running ... 2018-09-11 13:37:18.002 ERROR 16708---[pool-1-thread-1] com.fighting.SpringDynamicCronTask:dynamicCronTask is RU Nning ...
Related Article

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.