Use the @Scheduled annotation of the Spring framework to implement dynamic timing tasks

  1. Define the timing task interface
public interface ScheduledTask {
    
    

    /**
     * 执行定时任务
     */
    void executeTask();

    /**
     * 获取任务名称
     */
    String getTaskName();

    /**
     * 获取cron表达式
     */
    String getCronExpression();

    /**
     * 是否启用
     */
    boolean isEnabled();

}

  1. Implement the timing task interface
@Component
public class MyScheduledTask implements ScheduledTask {
    
    

    @Override
    @Scheduled(cron = "${my.task.cron}")
    public void executeTask() {
    
    
        // 执行定时任务的逻辑
    }

    @Override
    public String getTaskName() {
    
    
        return "myTask";
    }

    @Override
    public String getCronExpression() {
    
    
        return "0 0 0 * * ?";
    }

    @Override
    public boolean isEnabled() {
    
    
        return true;
    }

}

Using the ${} expression in the @Scheduled annotation, the cron expression can be read from the configuration file

  1. Define a scheduled task manager
@Component
public class ScheduledTaskManager {
    
    

    @Autowired
    private List<ScheduledTask> scheduledTasks;

    @Autowired
    private ThreadPoolTaskScheduler taskScheduler;

    private Map<String, ScheduledFuture<?>> scheduledFutures = new ConcurrentHashMap<>();

    /**
     * 启动所有定时任务
     */
    @PostConstruct
    public void startAllTasks() {
    
    
        for (ScheduledTask task : scheduledTasks) {
    
    
            if (task.isEnabled()) {
    
    
                ScheduledFuture<?> scheduledFuture = taskScheduler.schedule(task::executeTask, new CronTrigger(task.getCronExpression()));
                scheduledFutures.put(task.getTaskName(), scheduledFuture);
            }
        }
    }

    /**
     * 启动指定定时任务
     */
    public void startTask(String taskName) {
    
    
        ScheduledTask task = getTaskByName(taskName);
        if (task != null && !scheduledFutures.containsKey(taskName)) {
    
    
            ScheduledFuture<?> scheduledFuture = taskScheduler.schedule(task::executeTask, new CronTrigger(task.getCronExpression()));
            scheduledFutures.put(taskName, scheduledFuture);
        }
    }

    /**
     * 停止指定定时任务
     */
    public void stopTask(String taskName) {
    
    
        ScheduledFuture<?> scheduledFuture = scheduledFutures.get(taskName);
        if (scheduledFuture != null) {
    
    
            scheduledFuture.cancel(true);
            scheduledFutures.remove(taskName);
        }
    }

    /**
     * 根据任务名称获取定时任务
     */
    private ScheduledTask getTaskByName(String taskName) {
    
    
        for (ScheduledTask task : scheduledTasks) {
    
    
            if (task.getTaskName().equals(taskName)) {
    
    
                return task;
            }
        }
        return null;
    }

}

In the ScheduledTaskManager, inject all Beans that implement the ScheduledTask interface, and judge whether to enable the scheduled task according to the isEnabled() method. When starting a scheduled task, use the taskScheduler.schedule() method to add a scheduled task, and save the returned ScheduledFuture object in scheduledFutures. When stopping the scheduled task, stop it through the ScheduledFuture.cancel() method.

Guess you like

Origin blog.csdn.net/weixin_43749805/article/details/129711707