springBoot+scheduling实现多任务动态定时任务

 使用spring自带的scheduling定时调度任务相当于轻量级的Quartz,但是不支持分布式,若要实现分布式定时任务就得使用Quartz了.

第一步,在入口类中声明定时任务

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling //声明定时任务
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class,args);
    }
}

第二步,创建定时任务对象,需要实现SchedulingConfigurer接口,其中有两个入参doTask业务逻辑,getTrigger触发器

import com.zenithink.demo.controller.MyDynamicTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
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;

//@Component
public class ScheduledUtil implements SchedulingConfigurer {

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

    private  String cron="0/10 * * * * ?";

    private String name="测试";

    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;
    }

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.addTriggerTask(doTask(), getTrigger());
    }

    /**
     * 业务执行方法
     * @return
     */
    private Runnable doTask() {
        return new Runnable() {
            @Override
            public void run() {
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                // 业务逻辑
                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);
            }
        };
    }
}

第三步,创建多个定时任务对象,并注入spring容器

import com.zenithink.demo.utils.ScheduledUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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 org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.naming.Name;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class MyDynamicTask  {

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

    @Autowired
    @Qualifier("test1")
    private ScheduledUtil scheduledUtil1;

    @Autowired
    @Qualifier("test2")
    private ScheduledUtil scheduledUtil2;

    @Autowired
    @Qualifier("test3")
    private ScheduledUtil scheduledUtil3;


    @GetMapping
    public void getCron(String time,String name,Integer task) {
        if(task==1){
            scheduledUtil1.setCron(time);
            scheduledUtil1.setName(name);
        }else if(task==2){
            scheduledUtil2.setCron(time);
            scheduledUtil2.setName(name);
        }else if (task==3){
            scheduledUtil3.setCron(time);
            scheduledUtil3.setName(name);
        }
    }
}

执行日志示例:

 

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值