Dynamic add, cancel, change scheduled tasks

Mission objectives classes

import lombok.Data;
import org.springframework.stereotype.Service;

//要执行的任务目标类,任务就是打印id,将该类注册为bean,要在controller中多次使用
@Data
@Service
public class TaskRunnable implements Runnable{
private String id;
@Override
public void run(){
    System.out.println(this.getId());
}
}

Tools, including a new, cancel, change method

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ScheduledFuture;

//将该类注册为组件
@Component
public class MyTaskUtil {
/**
 * 将ScheduledFuture定义成静态的原因是
 * 如果有多个定时任务在执行时,取消方法不知道取消的是哪个
 * 要让上下文能连接上,所以用静态的
 */
private static Map<String,ScheduledFuture<?>>scheduledFutureMap=new HashMap<>();
//注入线程池的调度任务类
@Autowired
ThreadPoolTaskScheduler threadPoolTaskScheduler;
/**
 * 新增定时任务的方法
 * 入参1:时间表达式;入参2:定时任务的目标执行类
 */
public void addTask(String cron,TaskRunnable taskRunnable){
    //用触发器new一个时间表达式,创建的构造方法的入参就是时间表达式
    Trigger trigger=new CronTrigger(cron);
    /**
     * 第一个参数是定时任务要执行的目标类(就是到了定时的时间要执行什么);
     * 将触发任务放在线程池的任务调度里(第二个参数)
     * 该调用是有返回值的,返回类型是ScheduledFuture
     */
    ScheduledFuture<?> scheduledFuture=threadPoolTaskScheduler.schedule(taskRunnable,trigger );
    /**
     * 将目标任务添加到全局变量里,
     * 任务的id为key(其实就是一个标识,用来确定取消哪个任务,更改哪个任务的标识),这个key肯定是url请求的入参
     * 目标任务为value
     */
   scheduledFutureMap.put(taskRunnable.getId(), scheduledFuture);
}

/**
 * 取消定时任务的方法,
 * 入参是目标定时任务的id
 */
public void cancel(String id){
    //用全局变量map根据任务id获取任务,赋值给返回类型ScheduledFuture的变量
    ScheduledFuture<?> scheduledFuture=scheduledFutureMap.get(id);
    //调用cancel方法取消任务,传参true就是取消
    scheduledFuture.cancel(true);
}

/**
 * 更改定时任务
 * 参数1:定时任务时间表达式(多久执行一次)
 * 参数2:目标任务
 */
public void modify(String cron,TaskRunnable taskRunnable){
    //先调用取消方法,传入目标任务、任务id
    //再调用新增方法,传入时间表达式、目标任务
    this.cancel(taskRunnable.getId());
    this.addTask(cron,taskRunnable );
}
}

controller

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyTask {
//注入工具类,就是执行定时任务新增、更改、取消的类
@Autowired
MyTaskUtil taskUtil;
//注入任务目标类
@Autowired
TaskRunnable taskRunnable;
@RequestMapping("/addTask")
/**
 *新增定时任务的controller,传入任务id、表达式
 */
public void addTask(String id,String cron){
    //拼接时间表达式
    String s="0/"+cron+" * * * * ? ";
    //给目标任务setId
    taskRunnable.setId(id);
    //调用新增方法,传入时间表达式、目标任务(其实就是任务id)
    taskUtil.addTask(s,taskRunnable);
}

/**
 * 取消定时任务的controller,传入任务id
 */
@RequestMapping("/cancel")
public void cancel(String id){
    taskUtil.cancel(id);
}

/**
 * 修改定时任务的controller,与新增传参一致
 */
@RequestMapping("/modify")
public void modify(String id,String cron){
    //拼接时间表达式
    String s="0/"+cron+" * * * * ? ";
    //创建目标任务实例
    taskRunnable.setId(id);
    //调用新增方法,传入时间表达式、目标任务(其实就是任务id)
    taskUtil.modify(s,taskRunnable );
}
}

New first task

http://127.0.0.1:8080/addTask?id=1&cron=5      任务id为1,5秒执行一次

Add a second task

http://127.0.0.1:8080/addTask?id=2&cron=1      任务id为2,1秒执行一次

Cancel the second task

http://127.0.0.1:8080/cancel?id=2    取消任务id为2的任务,这个时候就只任务id为1的任务在继续执行

Change the task (task can only change being implemented, there is no open tasks, or can not find the task has been canceled task id's)

http://127.0.0.1:8080/modify?id=1&cron=1    更改任务id为1的任务,改为1秒执行一次,原本是5秒执行一次

Guess you like

Origin blog.csdn.net/qq_41767337/article/details/89405938