Spring Boot 动态定时任务(需要重启服务)

1.数据库表

2.配置类

package com.dmo.parkingview.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dmo.parkingview.pojo.TaskEntity;
import com.dmo.parkingview.service.ScheduledOfTask;
import com.dmo.parkingview.service.TaskEntityService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.util.Assert;

import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

@Configuration
public class ScheduledConfig implements SchedulingConfigurer {
    private ApplicationContext context;
    private TaskEntityService taskEntityService;

    public ScheduledConfig(ApplicationContext context,
                           TaskEntityService taskEntityService) {
        this.context = context;
        this.taskEntityService = taskEntityService;
    }

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        QueryWrapper<TaskEntity> taskEntityQueryWrapper = new QueryWrapper<>();
        List<TaskEntity> list = taskEntityService.list(taskEntityQueryWrapper);
        for (TaskEntity taskEntity : list) {
            Class<?> clazz;
            Object task;
            try {
                clazz = Class.forName(taskEntity.getCronKey());
                task = context.getBean(clazz);
            } catch (ClassNotFoundException e) {
                throw new IllegalArgumentException("task_entity表数据" + taskEntity.getCronKey() + "有误", e);
            } catch (BeansException e) {
                throw new IllegalArgumentException(taskEntity.getCronKey() + "未纳入到spring管理", e);
            }
            Assert.isAssignable(ScheduledOfTask.class, task.getClass(), "定时任务类必须实现ScheduledOfTask接口");
            // 可以通过改变数据库数据进而实现动态改变执行周期
            taskRegistrar.addTriggerTask(((Runnable) task),
                    triggerContext -> new CronTrigger(taskEntity.getCron()).nextExecutionTime(triggerContext)
            );
        }
    }

    @Bean
    public Executor taskExecutor() {
        return Executors.newScheduledThreadPool(10);
    }
}

3.自定义了一个接口ScheduledOfTask

package com.dmo.parkingview.service;

import com.dmo.parkingview.dto.Constant;
import com.dmo.parkingview.pojo.TaskEntity;
import com.dmo.parkingview.util.SpringUtil;

public interface ScheduledOfTask extends Runnable {
    /**
     * 定时任务方法
     */
    void execute();

    /**
     * 实现控制定时任务启用或禁用的功能
     */
    @Override
    default void run() {
        TaskEntityService taskEntityService = (TaskEntityService) SpringUtil.getBean(TaskEntityService.class);
        TaskEntity taskEntity = taskEntityService.findByCronKey(this.getClass().getName());
        if (Constant.TASK_STATUS == taskEntity.getStatus()) {
            execute();
        }
    }
}

4.定时任务

package com.dmo.parkingview.task;

import com.dmo.parkingview.service.ParkingImgService;
import com.dmo.parkingview.service.ScheduledOfTask;
import org.dom4j.DocumentException;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.text.ParseException;

/*
 * @Author jt
 * @Description //场库出入口图片,每周周日晚上十一点执行
 * @Date  2020/6/1
 * @Param
 * @return
 **/
@Component
public class ParkingImgTask implements ScheduledOfTask {

    private ParkingImgService parkingImgService;

    public ParkingImgTask(ParkingImgService parkingImgService) {
        this.parkingImgService = parkingImgService;
    }

    @Override
    public void execute() {
        try {
            parkingImgService.parkingImg();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值