SpringBoots实现定时任务
SpringBoot自带的Scheduled,可以将它看成一个轻量级的Quartz,使用起来比Quartz简单。
使用步骤(maven 工程):
1、pom.xml引入springboot.starter包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2、启动类通过注解@EnableScheduling,开启定时
3、创建定时任务实现类
4、参数说明
@Scheduled –参数可以接受两种定时的设置,一种是我们常用的cron="*/6 * ** * ?",一种是 fixedRate = 6000
例如上面两种都表示每隔六秒执行一次
fixedRate说明
@Scheduled(fixedRate =6000) :上一次开始执行时间点之后6秒再执行
@Scheduled(fixedDelay =6000) :上一次执行完毕时间点之后6秒再执行
@Scheduled(initialDelay=1000,fixedRate=6000) :第一次延迟1秒后执行,之后按fixedRate的规则每6秒执行一次