springboot定时任务基于注解demo
[code]import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling//开启对定时任务的支持 public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
[code]项目启动定时任务启动
[code]@Slf4j @Component public class Atask { @Scheduled(cron = "*/10 * * * * ?")//按照指定时间执行 public void aTask(){ log.info("10秒执行一次当前时间"); } @Scheduled(fixedRate = 5000)//fixedRate间隔固定的时间触发定时任务 public void aaa(){ log.info("5秒执行一次"); } }
根据设定的时间表达式触发定时任务
触发结果
2018-04-13 13:59:47.020 INFO 8240 --- [pool-1-thread-1] com.example.myproject.atask.Atask : 5秒执行一次
2018-04-13 13:59:50.002 INFO 8240 --- [pool-1-thread-1] com.example.myproject.atask.Atask : 10秒执行一次
2018-04-13 13:59:52.020 INFO 8240 --- [pool-1-thread-1] com.example.myproject.atask.Atask : 5秒执行一次
2018-04-13 13:59:57.020 INFO 8240 --- [pool-1-thread-1] com.example.myproject.atask.Atask : 5秒执行一次
2018-04-13 14:00:00.000 INFO 8240 --- [pool-1-thread-1] com.example.myproject.atask.Atask : 10秒执行一次
2018-04-13 14:00:02.021 INFO 8240 --- [pool-1-thread-1] com.example.myproject.atask.Atask : 5秒执行一次
2018-04-13 14:00:07.020 INFO 8240 --- [pool-1-thread-1] com.example.myproject.atask.Atask : 5秒执行一次
2018-04-13 14:00:10.002 INFO 8240 --- [pool-1-thread-1] com.example.myproject.atask.Atask : 10秒执行一次
2018-04-13 14:00:12.020 INFO 8240 --- [pool-1-thread-1] com.example.myproject.atask.Atask : 5秒执行一次
定时任务只需这两个注解即可
[code]@EnableScheduling,
[code]@Scheduled
定时任务表达式
阅读更多- 第四十章:基于SpringBoot & Quartz完成定时任务分布式多节点负载持久化
- 【转载】基于Spring Boot 实现定时任务
- spring task 定时任务 注解方式 demo
- spring基于注解实现定时任务
- 集群服务器下使用SpringBoot @Scheduled注解定时任务
- 基于Spring的多数据源(Druid)定时任务项目开发(一)<带Demo>
- spring boot 计划任务,定时任务的注解的使用@Scheduled@EnableScheduling
- spring自带的定时任务功能,基于注解和xml配置
- spring自带的定时任务功能,基于注解和xml配置(一)
- springboot之后,使用@schedule注解,开启定时任务
- spring定时任务-基于注解和xml
- 基于Spring boot定时任务编写的几种方法
- spring自带的定时任务功能,基于注解和xml配置
- 【译】Spring 4 基于TaskScheduler实现定时任务(注解)
- 基于Spring boot定时任务编写的几种方法
- 基于spring注解实现定时任务
- spring自带的定时任务功能,基于注解和xml配置
- 集群服务器下使用SpringBoot @Scheduled注解定时任务
- spring自带的定时任务功能,基于注解和xml配置
- spring自带的定时任务功能,基于注解和xml配置