设为首页 加入收藏

TOP

SpringBoot自定义cron表达式注册定时任务(一)
2023-07-25 21:30:41 】 浏览:53
Tags:SpringBoot cron 时任务

springBoot自定义cron表达式注册定时任务

一、原理

  • 1、使用Spring自带的TaskScheduler注册任务
  • 2、注册后返回:ScheduledFuture,用于取消定时任务
  • 3、注册任务后不会马上取消任务,所以将任务缓存。在需要取消任务的时候调用取消接口取消
  • 4、cron表达式可以由前端或者后端生成。实现中会校验cron表达式
public class TestScheduled {

    /**
     * 1、使用Spring自带的TaskScheduler注册任务
     * 2、注册后返回:ScheduledFuture,用于取消定时任务
     */
    @Resource
    private TaskScheduler taskScheduler;

    public void registrarTask() {
        //具体的任务Runnable(一般使用类实现Runnable接口)
        Runnable taskRunnable = new Runnable() {
            @Override
            public void run() {

            }
        };
        //cron表达式触发器
        CronTrigger trigger = new CronTrigger("0/5 * * * * ?");
        //开启定时任务的真正方法
        ScheduledFuture<?> future = this.taskScheduler.schedule(taskRunnable, trigger);
        //取消定时任务
        future.cancel(true);
    }
}

二、具体实现

1、配置任务调度器

  • 作用:设置:核心线程数:可同时执行任务数;设置线程名称前缀
  • 可以不配置。不配置就默认使用spring自带的
package com.cc.ssd.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

/** TaskScheduler任务调度器配置类
 * @since 2023/4/21 0021
 * @author CC
 **/
@Configuration
public class CronTaskConfig {

    /**
     * 任务调度器自定义配置
     */
    @Bean(name = "taskScheduler")
    public TaskScheduler taskScheduler() {
        // 任务调度线程池
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        // 定时任务执行线程池核心线程数:可同时执行4个任务
        taskScheduler.setPoolSize(4);
        taskScheduler.setRemoveOnCancelPolicy(true);
        // 线程名称前缀
        taskScheduler.setThreadNamePrefix("Cs-ThreadPool-");
        return taskScheduler;
    }

}

2、定时任务注册类

  • 作用:缓存、注册定时任务;还可以查询、删除定时任务
package com.cc.ssd.registrar;

import com.cc.ssd.task.CronTaskFuture;
import com.cc.ssd.task.CronTaskRunnable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.config.CronTask;
import org.springframework.scheduling.support.CronExpression;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/** 注册定时任务:缓存定时任务、注册定时任务到调度中心
 * @author CC
 **/
@Component
public class CronTaskRegistrar implements DisposableBean {

    private static final Logger log = LoggerFactory.getLogger(CronTaskRegistrar.class);

    /**
     * 缓存任务
     * key:具体的任务
     * value:注册定时任务后返回的ScheduledFuture
     */
    private final Map<Runnable, CronTaskFuture> scheduledTasks = new ConcurrentHashMap<>(16);

    /**
     * 使用自定义的任务调度配置
     */
    @Resource(name = "taskScheduler")
    private TaskScheduler taskScheduler;

    /** 获取任务调度配置
     * @return 任务调度配置
     */
    public TaskScheduler getTaskScheduler() {
        return this.taskScheduler;
    }

    /** 新增定时任务1
     *  存在任务:删除此任务,重新新增这个任务
     * @param taskRunnable 执行的具体任务定义:taskRunnable 实现Runnable
     * @param cronExpression cron表达式
     */
    public void addCronTask(Runnable taskRunnable, String cronExpression) {
        //验证cron表达式是否正确
        boolean validExpression = CronExpress
首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇win10环境下 VMware Workstation .. 下一篇微服务框架:如果不用 Spring Boo..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目