SpringBoot-- 定时任务 注解@Scheduled的使用
2020-07-14 04:30
411 查看
@Schedueld 定时任务
- 首先,在启动类中添加 @EnableScheduling 注解,开启定时任务
/** * 应用启动入口 * Created by pt on 2020/5/16. */ @SpringBootApplication @EnableScheduling //开启定时任务 public class PtApplication{ public static void main(String[] args) { SpringApplication.run(PtApplication.class, args); } }
- 然后创建定时任务类
@Component //启动类扫描组件 public class TaskTest { //@Scheduled(cron = "${scheduled.cron}") //通过配置yml文件统一管理运行时间 @Scheduled(cron = "0/3 * * * * ? ") //每3秒执行一次 public void testTask(){ System.out.println("执行定时任务:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); } }
- 若需要执行时间的统一管理,可以在自己的application.yml 文件中添加以下代码:
#定时任务执行时间 scheduled: cron: 0/3 * * * * ?
- 最后在自己的定时任务方法上引用即可
小结:
定时任务虽香,但是还存在一些小问题需要注意下;
- 不支持集群
单机没毛病,集群后,会有多个定时任务;
解决方案:使用某一台计算机节点,统一管理定时任务; - 会对数据库表进行全表查询
小型轻量级项目还可以,一旦数据量大了之后,会对性能造成很大影响;
附:
在线Cron表达式生成器
https://cron.qqe2.com/
相关文章推荐
- spring-boot通过@Scheduled配置定时任务及定时任务@Scheduled注解的方法
- Spring-boot定时任务,注解@Scheduled的参数说明
- 在Spring Boot中使用@Scheduled实现定时任务
- 在Spring项目中使用@Scheduled注解定义简单定时任务
- spring boot 计划任务,定时任务的注解的使用@Scheduled@EnableScheduling
- 使用spring的@Scheduled注解执行定时任务,启动项目不输出警告
- spring 第13天 使用@scheduled注解执行定时任务
- springboot之后,使用@schedule注解,开启定时任务
- spring 第13天 使用@scheduled注解执行定时任务
- 集群服务器下使用SpringBoot @Scheduled注解定时任务
- spring3.2使用@Scheduled注解完成定时任务
- 使用 Spring-boot 的 @Scheduled 来完成一个定时任务
- spring-boot实战【09】【转】:Spring Boot中使用@Scheduled创建定时任务
- SpringBoot使用@Scheduled创建定时任务
- Spring中使用注解 @Scheduled执行定时任务
- spring注解 @Scheduled(cron = "0 0 1 * * *")的使用来实现定时的执行任务
- Spring Boot中使用@Scheduled创建定时任务
- spring-boot 使用@Scheduled 开启定时任务
- 详解Spring Boot中使用@Scheduled创建定时任务
- 在Spring Boot中使用@Scheduled实现定时任务