一、简单使用
1.pom加入基本springboot基本的starter即可
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
2.@Scheduled
参数可以接受两种定时的设置,一种是我们常用的cron="*/6 * * * * ?"
,一种是 fixedRate = 6000
,两种都表示每隔六秒打印一下内容。
fixedRate 说明
-
@Scheduled(fixedRate = 6000)
:上一次开始执行时间点之后6秒再执行 -
@Scheduled(fixedDelay = 6000)
:上一次执行完毕时间点之后6秒再执行 -
@Scheduled(initialDelay=1000, fixedRate=6000)
:第一次延迟1秒后执行,之后按fixedRate的规则每6秒执行一次
@Component
public class TimingTask {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Scheduled(cron="*/6 * * * * ?")
private void process(){
System.out.println("this is scheduler task runing "+new Date());
}
@Scheduled(fixedRate = 6000)
public void reportCurrentTime() {
System.out.println("现在时间:" + dateFormat.format(new Date()));
}
}
3.启动类加上@EnableScheduling注解。
@SpringBootApplication(exclude = MongoAutoConfiguration.class)
@EnableScheduling
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
4.运行结果
this is scheduler task runing Thu Jul 18 10:59:06 CST 2019
现在时间:10:59:10
this is scheduler task runing Thu Jul 18 10:59:12 CST 2019
现在时间:10:59:16
this is scheduler task runing Thu Jul 18 10:59:18 CST 2019
现在时间:10:59:22
this is scheduler task runing Thu Jul 18 10:59:24 CST 2019
现在时间:10:59:28
以上就是定时任务的简单使用。但是有时候,定时任务需要关闭,和开启,或者修改定时任务的运行周期,可以使用下面的方式实现.
二、高级使用,自定义定时任务,关闭,开启,修改周期
1.说明
- ThreadPoolTaskScheduler:线程池任务调度类,能够开启线程池进行任务调度。
- ThreadPoolTaskScheduler.schedule()方法会创建一个定时计划ScheduledFuture,在这个方法需要添加两个参数,Runnable(线程接口类) 和CronTrigger(定时任务触发器)
- 在ScheduledFuture中有一个cancel可以停止定时任务。
@RestController
@RequestMapping("/time")
public class DynamicScheduledTask {
private static String DEFAULT_CRON = "0/5 * * * * ?";
@Autowired
private ThreadPoolTaskScheduler threadPoolTaskScheduler;
private ScheduledFuture<?> future;
@Bean
public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
return new ThreadPoolTaskScheduler();
}
@RequestMapping("/{id}/startCron")
public String startCron(@PathVariable("id") String id) {
future = threadPoolTaskScheduler.schedule(new MyRunnable(), new CronTrigger(DEFAULT_CRON));
System.out.println("DynamicTask.startCron()"+"------"+id);
return "startCron";
}
@RequestMapping("/{id}/stopCron")
public String stopCron(@PathVariable("id") String id) {
if (future != null) {
future.cancel(true);
}
System.out.println("DynamicTask.stopCron()"+"------"+id);
return "stopCron";
}
@RequestMapping("/{id}/changeCron10")
public String startCron10(@PathVariable("id") String id) {
stopCron(id);// 先停止,在开启.
future = threadPoolTaskScheduler.schedule(new MyRunnable(), new CronTrigger("*/10 * * * * *"));
System.out.println("DynamicTask.startCron10()"+"------"+id);
return "changeCron10";
}
private class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("DynamicTask.MyRunnable.run()," + new Date());
}
}
}
如果想,做成后台管理,添加删除定时任务,可以将定时任务,持久化到数据库,自定义开发MyRunnable定时任务的业务类,也持久化到数据库,然后,threadPoolTaskScheduler.schedule要的业务类,可通过反射实例化出来,传递,然后,通过url,id参数,来开启,关闭,删除,对应的定时任务。