参考文章:springboot自带定时器实现定时任务的开启关闭以及定时时间可以配置
一. 序言
最近项目需要用到定时任务,需要完成一个定时功能。经过了解,项目中目前实现定时任务,一般有三种选择:
- 一是用Java自带的timer类。稍微看了一下,可以实现大部分的指定频率的任务的调度(timer.schedule()),也可以实现关闭和开启(timer.cancle)。但是用其来实现某天的某个时间或者某月的某一天调度任务有点不方便。
- 二是采用Quartz 调度器实现。这是一个功能很强大的开源的专门用于定时任务调度的框架,也很好的和springboot整合,缺点:配置复杂,需要花费一定的时间去了解和研究。
- 三是spring3.0以后自带的scheduletask任务调度,可以实现quartz的大部分功能,不需要额外引用jar,也不需要另外配置。而且支持注解和配置文件两种。
我选择的是最直接简单的第三种。下面简单介绍下springboot自带的定时器来实现定时任务。
二. 开关任务代码
1. controller层代码
package com.left.controller;
import com.left.Result;
import com.left.runnable.MyRunnable1;
import com.left.runnable.MyRunnable2;
import com.left.util.ResultUtils;
import com.left.utils.YouXinConfiguration;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.concurrent.ScheduledFuture;
@Slf4j
@RestController
@Api(description = "定时任务")
@RequestMapping("/quartz/task")
public class DynamicTaskController {
@Autowired
private YouXinConfiguration youXinConfiguration;
@Autowired
private ThreadPoolTaskScheduler threadPoolTaskScheduler;
private ScheduledFuture<?> future1;
@Bean
public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
return new ThreadPoolTaskScheduler();
}
@PostMapping("/startCron1")
@ApiOperation("开始定时任务1")
public Result startCron1() {
future1 = threadPoolTaskScheduler.schedule(new MyRunnable1(),new Trigger(){
@Override
public Date nextExecutionTime(TriggerContext triggerContext){
return new CronTrigger(youXinConfiguration.getCorn1()).nextExecutionTime(triggerContext);
}
});
System.out.println("DynamicTask.startCron1()");
return ResultUtils.success();
}
@PostMapping("/stopCron1")
@ApiOperation("关闭定时任务1")
public Result stopCron1() {
if (future1 != null) {
future1.cancel(true);
}
System.out.println("DynamicTask.stopCron1()");
return ResultUtils.success();
}
}
代码中所引用类的介绍:
- ThreadPoolTaskScheduler:线程池任务调度类,能够开启线程池进行任务调度。
- ThreadPoolTaskScheduler.schedule()方法会创建一个定时计划ScheduledFuture,在这个方法需要添加两个参数,Runnable(线程接口类) 和CronTrigger(定时任务触发器)
- YouXinConfiguration:自定义读取yml文件中数据的类,通过该类读取yml文件中cron时间表达式,从而可以达到定时时间可配置的效果。
- MyRunnable1与MyRunnable2类:这两个类都是实现了Runnable接口,重写了run方法,定时任务的逻辑代码就是在其中实现。
2. YouXinConfiguration类代码
package com.left.utils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "youxing")
@EqualsAndHashCode(callSuper = false)
public class YouXinConfiguration {
private String corn1;
private String corn2;
}
3. yml文件(定时时间的配置的在最下面的youxing字段)
server:
port: 8002
spring:
application:
name: dubbo-client-orange
youxing:
corn1: 0/10 * * * * ?
corn2: 0/5 * * * * ?
4. MyRunnable1与MyRunnable2基本一样
// ============== MyRunnable1 =============== //
package com.left.runnable;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyRunnable1 implements Runnable {
@Override
public void run() {
System.out.println("first DynamicTask,"
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
}
// ============== MyRunnable2 =============== //
package com.left.runnable;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MyRunnable2 implements Runnable {
@Override
public void run() {
System.out.println("second DynamicTask,"
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
}
5. 启动SpringBoot项目,访问 http://localhost:8080/swagger-ui.html
6. 控制台打印效果