Spring定时任务

话不多说直接上代码!!!

其实静态的定时任务挺简单的

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.SimpleDateFormat;
import java.util.Date;

@Configuration
@EnableScheduling
public class DingSHi {

    @Scheduled(cron = "0/1 * * * * ?")//这个一看就知道是设置在多久时间执行一次的规则
    public void run(){
        //方法里面写间隔时间内需要执行的逻辑代码
        System.err.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }

}

动态定时任务的话就会有些麻烦,需要用到数据库...

先来一张cron表,表名和列名都叫这个好了,实在想不到什么名字。

来个规则model:

public class Cron {

    private String cron;

    public String getCron() {
        return cron;
    }

    public void setCron(String cron) {
        this.cron = cron;
    }
}

来个规则mapper:

import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;

@Service
public interface CronMapper {

    @Select("select cron from Cron limit 0,1")//只需要查第一个就好了
    String getCron();

}

来个规则service:

public interface CronService {

    String getCron();

}

来个规则serviceImpl:

import com.hxb.mapper.CronMapper;
import com.hxb.service.CronService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CronServiceImpl  implements CronService {

    @Autowired
    CronMapper mapper;


    @Override
    public String getCron() {
        return mapper.getCron();
    }
}

最后来一个调用者就OK了:

import com.hxb.mapper.CronMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
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.util.StringUtils;

import java.time.LocalDateTime;


@Configuration
@EnableScheduling
public class TryController implements SchedulingConfigurer {

    @Autowired
    @SuppressWarnings("all")//写上它无视编译错误
    CronMapper cronMapper;


    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addTriggerTask(
                //1.添加任务内容(Runnable)
                () -> outAPI(),
                //2.设置执行周期(Trigger)
                triggerContext -> {
                    //2.1 从数据库获取执行周期
                    String cron = cronMapper.getCron();
                    //2.2 合法性校验.
                    if (StringUtils.isEmpty(cron)) {
                        // Omitted Code ..
                    }
                    //2.3 返回执行周期(Date)
                    return new CronTrigger(cron).nextExecutionTime(triggerContext);
                }
        );
    }

    public void outAPI(){
        System.out.println("执行动态定时任务: " + LocalDateTime.now().toLocalTime());
    }


}

我们只要update数据库里面的规则就可以实现动态定时任务了

测试:

cron这个值自己根据需要百度就行了,反正我也不想明白。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值