本文最后更新于:October 2, 2022 am
SpringBoot框架中有两个非常重要的策略:开箱即用和约定优于配置。其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。
目录
导入依赖
| <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
|
配置文件
定时任务文件
在和application.yml同目录下新建一个文件 task-config.ini:
| printTime.cron=0/10 * * * * ?
|
方式一
开启定时任务
设置启动类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.tothefor.scheding;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling;
@EnableScheduling @SpringBootApplication public class SchedingApplication {
public static void main(String[] args) { SpringApplication.run(SchedingApplication.class, args); System.out.println("发射🚀"); }
}
|
定时任务执行类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| package com.tothefor.scheding.Sech;
import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; 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.time.LocalDateTime; import java.util.Date;
@Data @Component @PropertySource("classpath:/task-config.ini") public class ScheduleTask implements SchedulingConfigurer { private static final Logger LOGGER = LoggerFactory.getLogger(ScheduleTask.class);
@Value("${printTime.cron}") private String cron;
@Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { taskRegistrar.addTriggerTask(new Runnable() { @Override public void run() { LOGGER.info("TTF:Current time: {}", LocalDateTime.now()); } }, new Trigger() { @Override public Date nextExecutionTime(TriggerContext triggerContext) { CronTrigger cronTrigger = new CronTrigger(cron); Date nextExecutionTime = cronTrigger.nextExecutionTime(triggerContext); return nextExecutionTime; } }); } }
|
动态修改接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| package com.tothefor.scheding.Sech;
import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
@RestController @RequestMapping("/test") public class TestController { private static final Logger LOGGER = LoggerFactory.getLogger(TestController.class); private final ScheduleTask scheduleTask;
@Autowired public TestController(ScheduleTask scheduleTask) { this.scheduleTask = scheduleTask; }
@GetMapping("/updateCron") public String updateCron(String cron) { LOGGER.info("new cron :{}", cron); scheduleTask.setCron(cron); return "ok"; } }
|
测试
先将项目跑起来,可以看见控制台上的日志打印为每10秒打印一次。然后请求接口修改定时任务的执行周期:
| http://localhost:8081/test/updateCron?cron=0/15 * * * * ?
|
执行后,发现日志打印为每15秒打印一次。
方式二
上面的借助cron表达式的方法,还有另一种触发器,区别于CronTrigger触发器,该触发器可随意设置循环间隔时间,不像cron表达式只能定义小于等于间隔59秒。
定时任务执行类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| package com.tothefor.scheding.sc2;
import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; 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.PeriodicTrigger; import org.springframework.stereotype.Component;
import java.time.LocalDateTime; import java.util.Date;
@Data @Component public class ScheduleTask implements SchedulingConfigurer { private static final Logger LOGGER = LoggerFactory.getLogger(ScheduleTask.class);
private Long timer = 10000L;
@Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { taskRegistrar.addTriggerTask(new Runnable() { @Override public void run() { LOGGER.info("TTF:Current time: {}", LocalDateTime.now()); } }, new Trigger() { @Override public Date nextExecutionTime(TriggerContext triggerContext) {
PeriodicTrigger periodicTrigger = new PeriodicTrigger(timer); Date nextExecutionTime = periodicTrigger.nextExecutionTime(triggerContext); return nextExecutionTime; } }); } }
|
动态修改接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| package com.tothefor.scheding.Sech;
import com.tothefor.scheding.sc2.ScheduleTask; import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
@RestController @RequestMapping("/test") public class TestController { private static final Logger LOGGER = LoggerFactory.getLogger(TestController.class);
private final ScheduleTask scheduleTask;
@Autowired public TestController(ScheduleTask scheduleTask) { this.scheduleTask = scheduleTask; }
@GetMapping("/updateTimer") public String updateTimer(Long timer) { LOGGER.info("new timer :{}", timer); scheduleTask.setTimer(timer); return "ok"; } }
|
📢注意:不要Autowired注入错了!!!要把方式一的类给注释掉!!
总结
- 使用CronTrigger触发器:只能定义定时任务的间隔。
- 使用PeriodicTrigger触发器:能自定义间隔时长。