springboot动态设置定时任务

环境变量中设置执行时间, 

单个执行定时任务:

import com.ksyun.alertmanager.service.AlertManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
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.config.Task;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.Map;

@Component
@Configuration
@EnableScheduling
public class AlertCriticalTask implements SchedulingConfigurer {


    public static String cron;

    @Autowired
    private AlertManagerService alertManagerService;

    /**
     * Callback allowing a {@link TaskScheduler
     * TaskScheduler} and specific {@link Task Task}
     * instances to be registered against the given the {@link ScheduledTaskRegistrar}.
     *
     * @param taskRegistrar the registrar to be configured.
     */
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {

        //项目部署时,会在这里执行一次,从数据库拿到cron表达式
        Map<String, String> getenv = System.getenv();
        cron = getenv.get("alertmanager.critical");
        //cron = "*/30 * * * * ?";
        if (cron == null){
            cron = "0 0 0,23 * * ?";
        }

        Runnable task = new Runnable() {
            @Override
            public void run() {
                //任务逻辑代码部分.
                System.out.println("*********************告警信息处理服务--30秒执行一次*********************");
                try {
                    //执行的实现类
                    alertManagerService.getDataResponses();
                } catch (ParseException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };

        Trigger trigger = new Trigger() {
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                //任务触发,可修改任务的执行周期.
                //每一次任务触发,都会执行这里的方法一次,重新获取下一次的执行时间
                cron = getenv.get("alertmanager.critical");
                //cron = "*/30 * * * * ?";
                CronTrigger trigger = new CronTrigger(cron);
                Date nextExec = trigger.nextExecutionTime(triggerContext);
                return nextExec;
            }
        };
        taskRegistrar.addTriggerTask(task, trigger);

    }
}

 循环遍历,批量执行定时任务:

import com.ksyun.alertmanager.service.AlertManagerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
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.config.Task;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
@Configuration
@EnableScheduling
public class TaskList implements SchedulingConfigurer {

    @Autowired
    private AlertManagerService alertManagerService;

    /**
     * Callback allowing a {@link TaskScheduler
     * TaskScheduler} and specific {@link Task Task}
     * instances to be registered against the given the {@link ScheduledTaskRegistrar}.
     *
     * @param taskRegistrar the registrar to be configured.
     */
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {

        List<String> stringList = new ArrayList<>();
        //项目部署时,会在这里执行一次,从数据库拿到cron表达式
        Map<String, String> getenv = System.getenv();
        /*cron = getenv.get("alertmanager.critical");
        if (cron == null){
            cron = "0 0 0,23 * * ?";
        }*/
        //cron = "*/30 * * * * ?";
        /*stringList.add(getenv.get("alertmanager.critical"));
        stringList.add(getenv.get("alertmanager.none"));
        stringList.add(getenv.get("alertmanager.warning"));*/

        stringList.add("*/10 * * * * ?");
        stringList.add("*/30 * * * * ?");
        stringList.add("*/50 * * * * ?");

        stringList.forEach(cron ->{

            Runnable task = new Runnable() {
                @Override
                public void run() {
                    //任务逻辑代码部分.
                    log.info("*********************告警信息处理服务" + cron + "级别的告警信息执行开始*********************");
                    try {
                        alertManagerService.getDataResponses(cron);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            };

            Trigger trigger = new Trigger() {
                @Override
                public Date nextExecutionTime(TriggerContext triggerContext) {
                    //任务触发,可修改任务的执行周期.
                    //每一次任务触发,都会执行这里的方法一次,重新获取下一次的执行时间
                    //cron = getenv.get("alertmanager.critical");
                    //cron = "*/30 * * * * ?";
                    CronTrigger trigger = new CronTrigger(cron);
                    Date nextExec = trigger.nextExecutionTime(triggerContext);
                    return nextExec;
                }
            };
            taskRegistrar.addTriggerTask(task, trigger);
        });

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值