Spring Boot -08 定时任务
基本概念:
在未来的特定时间段执行指定的任务
SpringBoot使用注解方式开启定时任务:
(1)启动类中添加 @EnableScheduling 注解,开启定时任务,自动扫描
// @EnableScheduling注解的作用是开启定时任务
@EnableScheduling
//开启异步
@EnableAsync
@SpringBootApplication
@RestController
@EnableSwagger2
@ServletComponentScan
public class App extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(App.class);
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
(2)开启定时任务
@Component
public class VideoOrderTask {
// fixedDelay,执行结束后再过4秒执行一次
@Scheduled(fixedDelay = 400)
public void suma(){
System.out.println("当前剩余车票为:"+);
// 休息2秒
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
源码地址:https://download.csdn.net/download/qq_41781775/19148444