Springboot realizes dynamic setting of timing tasks

1. Background introduction

In the actual design and development of the "test set" function, a cron expression field for a timed task is provided for each test set. The code needs to realize that the test set with cron expression is dynamically added to the timed task, according to the cron expression Regularly execute the interface use cases in the test set

Two, Springboot implementation process

2.1 Create ScheduledConfig configuration class

package com.test.testmanagement.config;

import com.test.testmanagement.controller.casemanagementcontroller.InterfaceCaseTestSuiteController;
import com.test.testmanagement.model.interfaceBean.InterfaceCaseTestSuite;
import com.test.testmanagement.service.InterfaceCaseTestSuiteService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;

import java.util.List;

/**
 * @description:
 * @author: XXX
 * @time: 2021-02-09 15:42
 */
@Configuration
public class ScheduledConfig implements SchedulingConfigurer {

    @Autowired
    private TaskScheduler myThreadPoolTaskScheduler;

    @Autowired
    private InterfaceCaseTestSuiteService interfaceCaseTestSuiteService;

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setTaskScheduler(myThreadPoolTaskScheduler);

        try{
            //获取所有的设置定时任务的测试集
            List<InterfaceCaseTestSuite> interfaceCaseTestSuiteList = interfaceCaseTestSuiteService.findAllInterfaceCaseTestSuiteWithCrontab();
            System.out.println("需要执行定时任务的测试集个数为:" + interfaceCaseTestSuiteList.size());
            InterfaceCaseTestSuiteController interfaceCaseTestSuiteController = new InterfaceCaseTestSuiteController();
            for(InterfaceCaseTestSuite testSuite : interfaceCaseTestSuiteList){
                //可以实现动态调整定时任务的执行频率
                taskRegistrar.addTriggerTask(
                        // 1、添加任务内容(Runnable)
//                        () -> System.out.println("cccccccccccccccc--->" + Thread.currentThread().getId()),
                        new Runnable() {
                            @Override
                            public void run() {
                                try{
                                    interfaceCaseTestSuiteController.debugInterfaceCaseTestSuite(testSuite.getId(), testSuite.getTestSuiteName());
                                }catch (Exception e){
                                    e.printStackTrace();
                                }
                            }
                        },
                        // 2、设置执行周期(Trigger)
                        triggerContext -> {
                            //2.1 从数据库动态获取执行周期
                            String cron = testSuite.getTestSuiteCronExpression();
                            //2.2 合法性校验.
                            if (StringUtils.isEmpty(cron)) {
                                // Omitted Code ..
                                System.out.println("计划任务为空");
                            }
                            //2.3 返回执行周期(Date)
                            return new CronTrigger(cron).nextExecutionTime(triggerContext);
                        }
                );
            }
        }catch (Exception e){
            System.out.println("动态定时任务出现异常!!");
            e.printStackTrace();
        }
    }
}

2.2 Restart the project, and the scheduled task will be automatically added

There is a problem:
when the cron expression of a test set changes, the project still needs to be restarted to be effective, and it does not meet the requirements of the change, that is, to dynamically update the timing task

Guess you like

Origin blog.csdn.net/sinat_34241861/article/details/113842306