SpringBoot动态设置定时任务

1、基础的定时任务实体类,用于对应存储数据库

@Data
public class MyJob {
    
    private Integer id;
    //任务名
    private String jobName;
    //分组
    private String jobGroup;
    //执行的目标类
    private String invokeTarget;
    //是否启用状态
    private Integer status;
    //定时cron表达式
    private String cron;

    public MyJob(Integer id, String jobName, String jobGroup, String invokeTarget, Integer status,String cron) {
        this.id = id;
        this.jobName = jobName;
        this.jobGroup = jobGroup;
        this.invokeTarget = invokeTarget;
        this.status = status;
        this.cron = cron;
    }
}

2、工具栏,用于从spring容器中取出bean

@Component
public class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware {

    private static ConfigurableListableBeanFactory beanFactory;
    private static ApplicationContext context;

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        SpringUtils.beanFactory = configurableListableBeanFactory;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringUtils.context = applicationContext;
    }

    /**
     * 从上下文中获取bean
     * @param className
     * @param <T>
     * @return
     */
    public static<T> T getBean(String className){
        return (T)beanFactory.getBean(className);
    }
}

3、要执行的定时任务

@Service("TaskService")
public class TaskService implements Job {

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("定时任务执行...");
    }
}

4、动态构建定时任务

@Component
public class ScheduleJob {

    public void createJob(MyJob myJob, Scheduler scheduler) throws SchedulerException {
        //定时任务要执行的类
        String className = myJob.getInvokeTarget();
        //获取spring容器中的类
        Class<? extends Job> aClass = ((Job)SpringUtils.getBean(className)).getClass();
        //任务详情
        JobDetail jobDetail = JobBuilder.newJob(aClass)
                .withIdentity(myJob.getJobName(),myJob.getJobGroup())
                .build();
        //执行时间
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(myJob.getCron())
                .withMisfireHandlingInstructionFireAndProceed();
        //触发器
        CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                .withIdentity(myJob.getJobName(),myJob.getJobGroup())
                .withSchedule(cronScheduleBuilder)
                .build();
        //绑定任务和触发器
        scheduler.scheduleJob(jobDetail,cronTrigger);
    }
}

5、单元测试

@SpringBootTest
@EnableScheduling
public class UserInfoServiceTest<T> {

    @Autowired
    ScheduleJob scheduleJob;
    @Autowired
    Scheduler scheduler;

    @Test
    public void test(){
        MyJob job = new MyJob(1,"job-1","default","TaskService",1,"0/10 * * * * ? ");
        try {
            scheduleJob.createJob(job,scheduler);
            Thread.sleep(60*1000*10);
        } catch (SchedulerException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值