spring task实现动态定时任务

1.在启动类中声明定时任务 :@EnableScheduling //声明定时任务

spring task实现动态定时任务

2.定时任务: 

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
 
/**
 * @author Administrator
 */
@Component
@Slf4j
public class ScheduledUtil implements SchedulingConfigurer {
 
    /**
     * cron表达式
     */
    private  String cron="0/10 * * * * ?";
    /**
     * 任务名称
     */
    private String name="test";
    /**
     * 自定义参数
     */
    private String jobData="";
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getCron() {
        return cron;
    }
 
    public void setCron(String cron) {
        this.cron = cron;
    }
 
    public String getJobData() {
        return jobData;
    }
 
    public void setJobData(String jobData) {
        this.jobData = jobData;
    }
 
    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.addTriggerTask(doTask(jobData), getTrigger());
    }
 
    /**
     * 业务执行方法
     * @return
     */
    private Runnable doTask(String jobData) {
        return new Runnable() {
            @Override
            public void run() {
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                log.info("jobData"+jobData);
                // 业务逻辑
                log.info(name+",时间为:" + simpleDateFormat.format(new Date()));
            }
        };
    }
 
    /**
     * 业务触发器
     * @return
     */
    private Trigger getTrigger() {
        return new Trigger() {
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                // 触发器
                CronTrigger trigger = new CronTrigger(cron);
                return trigger.nextExecutionTime(triggerContext);
            }
        };
    }
}

3.测试修改触发器接口类,调用完接口后定时器时间就会从10秒一次变为30秒一次。

这样就不需要修改cron配置重启服务了,只需要调用接口设置相应的cron就可以了

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/task")
@Api(tags = "task", description = "task任务")
public class MyDynamicTask  {

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

    @Autowired
    private ScheduledUtil scheduledUtil;



    @PostMapping("/update")
    @ApiOperation(value = "修改task任务")
    public void getCron(String name,String cron,String jobData) {
        scheduledUtil.setName(name);
        // "0/30 * * * * ?"
        scheduledUtil.setCron(cron);
        scheduledUtil.setJobData(jobData);
    }
}

4.测试截图

spring task实现动态定时任务

spring task实现动态定时任务