欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot项目中使用@Scheduled读取动态参数

程序员文章站 2022-03-28 22:00:39
目录使用@scheduled读取动态参数1、基于@scheduled可配置开发2、基于代码实现spring boot scheduled动态配置使用@scheduled读取动态参数1、基于@sched...

使用@scheduled读取动态参数

1、基于@scheduled可配置开发

application.propertites: 
read.timer.parmas=0 0/1 * * * *

定时类:

@component
public class scheduledservice {
logger logger= loggerfactory.getlogger(scheduledservice.class);
    @scheduled(cron = "${read.timer.parmas}")
    public void readconfigtable(){
        logger.info("*****.read.timer.parmas");
    }
}

启动类:

@springbootapplication
@enablescheduling  //必须
public class dataapplication {
  public static void main(string[] args) {
        springapplication.run(dataapplication.class,args);
    }
}

2、基于代码实现

(1)核心代码

@component
@enablescheduling
public class testscheduledparams implements schedulingconfigurer{
    logger logger= loggerfactory.getlogger(testscheduledparams.class);
 
    public static  string default_corn="0/3 * * * * *";
    //##动态传参要给默认值。
    public static string corn=default_corn;
 
    @override
    public void configuretasks(scheduledtaskregistrar taskregistrar) {
     
        taskregistrar.addtriggertask(new runnable() {
            @override
            public void run() {
               // logger.info("定时任务逻辑");
            }
        }, new trigger() {
            @override
            public date nextexecutiontime(triggercontext triggercontext) {
                //任务触发,可修改任务的执行周期
                crontrigger crontrigger = new crontrigger(corn);
 
                date date = crontrigger.nextexecutiontime(triggercontext);
                return date;
            }
        });
    }
}

(2)其他类或方法动态传参赋值

testscheduledparams.corn="0/20 * * * * *"

spring boot scheduled动态配置

package cn.com.suntree.cmp.service;
import cn.com.suntree.cmp.utils.commonutil;
import lombok.extern.log4j.log4j2;
import org.apache.commons.lang3.stringutils;
import org.springframework.beans.factory.annotation.autowired;
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 redis.clients.jedis.jedis;
import java.util.date;
@component
@log4j2
public class mydynamictask implements schedulingconfigurer {//实现schedulingconfigurer 接口
    @autowired
    commonutil commonutil;
    @autowired
    taskservice taskservice;
    private static string cron;
    @override
    public void configuretasks(scheduledtaskregistrar scheduledtaskregistrar) {//重写该方法
        scheduledtaskregistrar.addtriggertask(dotask(), gettrigger());
    }
    private runnable dotask() {
        return new runnable() {
            @override
            public void run() {
                log.info("-------------------------------执行"+cron);
                taskservice.monday();//真正需要运行的逻辑代码
            }
        };
    }
    private trigger gettrigger() {
        return new trigger() {
            @override
            public date nextexecutiontime(triggercontext triggercontext) {
                // 触发器
                crontrigger trigger = new crontrigger(getcron());
                return trigger.nextexecutiontime(triggercontext);
            }
        };
    }
    public string getcron() {
        jedis jedis = commonutil.getjedis();
        string newcron =  jedis.get("cron");//可以改成配置到数据库中
        if (stringutils.isempty(newcron)) {
            jedis.set("cron","0 30 0 ? * mon");
            return "0 30 0 ? * mon";
        }
        if (!newcron.equals(cron)) {
            log.info(new stringbuffer("cron has been changed to:'").append(newcron).append("'. old cron was:'").append(cron).append("'").tostring());
            cron = newcron;
        }
        return cron;
    }
}

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