spring schedule实现动态配置执行时间

2022-07-21,,,,

目录

spring schedule 动态配置执行时间

之前saas平台实现动态修改定时任务的时间,都是通过xx-job这样的框架来实现,这样我们可以单独一个服务来管理我们整个saas平台的定时任务,但是最近给银行做的一个小项目,需要本地化部署,所以我不想弄很多的服务,并且他们并没有要求修改以后即时生效,所以我直接采用了 spring schedule结合mysql动态配置执行时间。

之前我们用的schedule通过注解的方式,只能用静态的corn表达式,如果想实现动态的需要实现schedulingconfigurer,并且通过注解@enablescheduling。如下:

package com.zqf.marketing.task;  
import com.zqf.db.marketingrobot.sys.model.robotsysswitch;
import com.zqf.marketing.sys.service.switchservice;
import org.slf4j.logger;
import org.slf4j.loggerfactory;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.context.annotation.lazy;
import org.springframework.scheduling.trigger;
import org.springframework.scheduling.triggercontext;
import org.springframework.scheduling.annotation.enablescheduling;
import org.springframework.scheduling.annotation.schedulingconfigurer;
import org.springframework.scheduling.config.scheduledtaskregistrar;
import org.springframework.scheduling.support.crontrigger;
import org.springframework.stereotype.service; 
import java.util.date;
 
/**
 * @author zhenghao
 * @description
 * @date 2019/1/22 21:50
 */
 
@lazy(false)
@service
@enablescheduling
public class testtaskservice implements schedulingconfigurer { 
    private static logger log = loggerfactory.getlogger(testtaskservice.class);
    @autowired
    private switchservice switchservice; 
 
    private string springdynamiccrontask() {
        string cron = "0/5 * * * * ?";
        //从数据库获得配置的corn表达式
        robotsysswitch switchbyid = switchservice.getswitchbyid(5l);
        cron = switchbyid.getswitchflag();
        log.info(cron);
        return cron;
    } 
 
    @override
    public void configuretasks(scheduledtaskregistrar scheduledtaskregistrar) {
        scheduledtaskregistrar.addtriggertask(new runnable() {
            @override
            public void run() {
                // 任务逻辑
                log.info("task_task_tak");
            }
        }, new trigger() {
            @override
            public date nextexecutiontime(triggercontext triggercontext) {
                string s = springdynamiccrontask();
                // 任务触发,可修改任务的执行周期
                crontrigger trigger = new crontrigger(s);
                date nextexec = trigger.nextexecutiontime(triggercontext);
                return nextexec;
            }
        });  
    }
}

这样我们就可以动态的修改task的执行时间,生效时间为,上一个任务的执行周期,也可以满足我们现在需求,这样就可以实习项目更加的灵活!

@schedule注解动态配置时间间隔

动态配置时间间隔是通过自己实现的任务注册到任务调度实现的,并在每次调度的时候更改下次调度时间间隔,如果任务阻塞或者挂掉了就不会再被调度了,如果设置时间过长,到下次调度就需要等待很长时间。

import org.springframework.beans.factory.annotation.autowired;
import org.springframework.scheduling.trigger;
import org.springframework.scheduling.triggercontext;
import org.springframework.scheduling.annotation.enablescheduling;
import org.springframework.scheduling.annotation.schedulingconfigurer;
import org.springframework.scheduling.config.scheduledtaskregistrar;
import org.springframework.scheduling.support.periodictrigger;
import org.springframework.stereotype.component; 
import java.util.date; 
 
@component
@enablescheduling
public class dynamicscheduletasksecond implements schedulingconfigurer {
    private static final long week_millis = 604800000;
    private static final long min_millis = 1000;
    private static long period = 1000;
    static long l = system.currenttimemillis(); 
    @autowired
    setperiod setperiod;
 
    public static long getperiod() {
        return period;
    }
 
    public static void setperiod(long period) {
        dynamicscheduletasksecond.period = period;
    }
 
    @override
    public void configuretasks(scheduledtaskregistrar taskregistrar) {
        taskregistrar.addtriggertask(new runnable() {
            @override
            public void run() {
                try {
                    setperiod.update(period);
                    system.out.println("abc");
                    long last = system.currenttimemillis() - l;
                    l = system.currenttimemillis();
                    system.out.println(last);
                } catch (exception e) {
                    e.printstacktrace();
                }
            }
        }, new trigger() {
            @override
            public date nextexecutiontime(triggercontext triggercontext) {
                if (period < min_millis || period > week_millis)
                    period = min_millis;
                periodictrigger periodictrigger = new periodictrigger(period);
                date nextexecdate = periodictrigger.nextexecutiontime(triggercontext);
                return nextexecdate;
            }
        });
    }
}
 
import org.springframework.stereotype.component; 
@component
public class setperiod { 
    private static long maxperiod = 1000l; 
    public void update(long period) {
        maxperiod += 1000;
        setscheduleconfig(maxperiod);
    }
 
    public boolean setscheduleconfig(long period) {
        dynamicscheduletasksecond.setperiod(period);
        return true;
    }
}

上面是实现动态调度的一个简单实例,下面说一下基本原理。

动态调度功能主要是实现schedulingconfigurer函数式接口,接口中的方法configuretasks的参数是重点。

@functionalinterface
public interface schedulingconfigurer { 
 /**
  * callback allowing a {@link org.springframework.scheduling.taskscheduler
  * taskscheduler} and specific {@link org.springframework.scheduling.config.task task}
  * instances to be registered against the given the {@link scheduledtaskregistrar}.
  * @param taskregistrar the registrar to be configured.
  */
 void configuretasks(scheduledtaskregistrar taskregistrar); 
}

看名字 scheduledtaskregistrar就知道是一个调度任务注册类,调用这个类的addtriggertask方法需要两个参数

    public void addtriggertask(runnable task, trigger trigger) {
        this.addtriggertask(new triggertask(task, trigger));
    }

一个是任务线程这个最后说,先说一下第二个trigger,这是一个设置任务触发时间的接口,具体的实现有两个类,一个是crontrigger对应的就是cron类型的时间设置,一个是periodictrigger对应的就是fixdelay和fixrate两种方式的时间设置,实例中使用的是后者。

public interface trigger {
    @nullable
    date nextexecutiontime(triggercontext var1);
}

接口方法参数是一个triggercontext,这个参数就是任务触发的上下文,里面保存着上一次任务开始时间和结束时间和实际执行用时,自己需要实现这个nextexecutiontime方法根据上一次任务执行时间来返回一个新的date时间,new一个新的periodictrigger对象初始化period时间间隔为新的时间间隔用nextexecutiontime方法就可以了根据上下文时间返回一个新的任务调度时间了,但是period的时间不能太长也不能太短最好设置一个区间,这样可以避免很多粗心的错误导致的麻烦,到此完美解决动态设置任务调度时间间隔功能。

再说一下第一个线程任务中的需要做的事,执行的任务需要在其他的具体类中实现,然后在这个线程中调用,然后每次在调度任务的时候就要根据时间业务重新设置时间间隔,比如读配置后改变时间间隔,也就是调度和具体的任务形成一个环,调度执行具体的任务后,具体的任务在设置调度的时间间隔。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

《spring schedule实现动态配置执行时间.doc》

下载本文的Word格式文档,以方便收藏与打印。