SpringBoot scheduled task upgrade articles (dynamically add, modify, delete scheduled tasks)

Origin of demand: After the release of "Spring Boot Scheduled Task Upgrade", I got a lot of feedback, one of which is how to dynamically add, modify and delete scheduled tasks? So let's take a look at the specific implementation, first look at the outline of this section:

(1 ) Explanation of ideas;
( 2 ) Code analysis;
( 3) Special instructions for modifying the execution cycle of timed tasks;

Let's take a look at the outline of this section:

(1) Explanation of ideas;

(a) First of all, we need to re-understand a class ThreadPoolTaskScheduler: the thread pool task scheduling class, which can open the thread pool for task scheduling.

(b) The ThreadPoolTaskScheduler.schedule() method will create a timed schedule ScheduledFuture, in which two parameters need to be added, Runnable (thread interface class) and CronTrigger (timed task trigger)

(c) There is a cancel in ScheduledFuture to stop the scheduled task.

(2) Code analysis;

       According to the above analysis of ideas, we can easily know how to code. The code is provided as follows:

 

package com.kfit.task;

import java.util.Date;
import java.util.concurrent.ScheduledFuture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.scheduling.support.CronTrigger; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author Angel -- guardian angel * @version v.0.1 * @date April 6, 2017 */ @RestController @Component public class DynamicTask { @Autowired private ThreadPoolTaskScheduler threadPoolTaskScheduler; private ScheduledFuture<?> future; @Bean public ThreadPoolTaskScheduler threadPoolTaskScheduler() { return new ThreadPoolTaskScheduler(); } @RequestMapping("/startCron") public String startCron() { future = threadPoolTaskScheduler.schedule(new MyRunnable(), new CronTrigger("0/5 * * * * *")); System.out.println("DynamicTask.startCron()"); return "startCron"; } @RequestMapping("/stopCron") public String stopCron() { if (future != null) { future.cancel(true); } System.out.println("DynamicTask.stopCron()"); return "stopCron"; } @RequestMapping("/changeCron10") public String startCron10() { stopCron(); // Stop first, then start. future = threadPoolTaskScheduler.schedule( new MyRunnable(), new CronTrigger("*/10 * * * * *" )); System.out.println("DynamicTask.startCron10()"); return "changeCron10"; } private class MyRunnable implements Runnable { @Override public void run() { System.out.println("DynamicTask.MyRunnable.run()," + new Date()); } } }


Summary:
(a) We first created a class DynamicTask;
(b) Defined two variables, threadPoolTaskScheduler and future where future is the return value of the treadPoolTaskScheduler execution method schedule, which is mainly used for the stop of timed tasks.
(c) Write the method startCron() to start the timer;
(d) Write the stop method stopCron(), when coding here, you need to pay attention to when you need to judge that the future is null, otherwise it is easy to throw NullPointerException;
(e ) Write the method changeCron10() to modify the execution cycle of the timed task. The principle here is to close the previous timer, and the innovation is to create a new timer.

(3) Special instructions for modifying the execution cycle of timed tasks;

       In the previous blog, we used a way to modify the value of the cron parameter through global variables, so we can do the same here. Here is a simple idea for everyone to implement by themselves.

Note that the second parameter of schedule() in ThreadPoolTaskScheduler supports Trigger:

ThreadPoolTaskScheduler.schedule(Runnable arg0, Trigger arg1)

Then we can define a Trigger ourselves, and then modify it dynamically. The core code provided here is as follows:

private String cronStr = "*/5 * * * * *";

    @RequestMapping("/startCron1")

    public String startCron1(){

        System.out.println("startCron1 >>>>");

        threadPoolTaskScheduler.schedule(new MyRunnable(), new Trigger(){

            @Override
            public Date nextExecutionTime(TriggerContext triggerContext){
                returnnew CronTrigger(cronStr).nextExecutionTime(triggerContext);
            }
        });

        System.out.println("startCron1 <<<<");
        return "startCron1";
    }

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324893567&siteId=291194637