SpringBoot并发执行定时任务

刚刚看了下Spring Boot实现定时任务的文章,感觉还不错。Spring Boot 使用Spring自带的Schedule来实现定时任务变得非常简单和方便。在这里个大家分享下。

开启缓存注解

  1. @SpringBootApplication
  2. @EnableScheduling //开启定时任务
  3. public class Application {
  4. public static void main(String[] args) {
  5. SpringApplication.run(Application.class, args);
  6. }
  7. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

编写定时任务

  1. @Component
  2. public class ScheduledTasks {
  3. private Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);
  4. // cron接受cron表达式,根据cron表达式确定定时规则
  5. @Scheduled(cron= "0/5 * * * * ? ") //每5秒执行一次
  6. public void testCron() {
  7. DateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
  8. logger.info(sdf.format( new Date())+ "*********每5秒执行一次");
  9. }
  10. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

任务完成

启动项目,查看控制台打印信息,发现定时任务已经生效。spring boot 和Scheduled整合完毕。


1240

存在问题

但是后来发现个问题,通过同时测试几个任务发现,所有的任务都是在同一个线程池中的同一个线程来完成的。在实际开发过程中,我们当然不希望所有的任务都运行在一个线程中。

  1. @Scheduled(cron= "0/1 * * * * ? ") //每1秒执行一次
  2. public void testCron1() {
  3. DateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
  4. logger.info(sdf.format( new Date())+ "*********每1秒执行一次");
  5. }
  6. @Scheduled(cron= "0/2 * * * * ? ") //每2秒执行一次
  7. public void testCron2() {
  8. DateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
  9. logger.info(sdf.format( new Date())+ "*********每2秒执行一次");
  10. }
  11. @Scheduled(cron= "0/3 * * * * ? ") //每3秒执行一次
  12. public void testCron3() {
  13. DateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
  14. logger.info(sdf.format( new Date())+ "*********每3秒执行一次");
  15. }
  16. @Scheduled(cron= "0/4 * * * * ? ") //每4秒执行一次
  17. public void testCron4() {
  18. DateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
  19. logger.info(sdf.format( new Date())+ "*********每4秒执行一次");
  20. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
1240

解决方案

那么,怎么设计成多线程实现并发呢?在网上看到过这样的解决方案。通过ScheduleConfig配置文件实现SchedulingConfigurer接口,并重写setSchedulerfang方法,我们尝试着配置了一下。

  1. @Configuration
  2. public class ScheduleConfig implements SchedulingConfigurer {
  3. @Override
  4. public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
  5. taskRegistrar.setScheduler(Executors.newScheduledThreadPool( 5));
  6. }
  7. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

整合成功

这样就完成了多线程并发的配置?我们启动项目通过控制台输出信息验证一下结果,最后发现所有的任务都在同一个线程池但不同线程中完成,说明这个方案完全可行,这样,我们就完成了spring boot 多线程并发定时任务。


1240

@Scheduled所支持的参数:

1.cron:cron表达式,指定任务在特定时间执行;
2.fixedDelay:表示上一次任务执行完成后多久再次执行,参数类型为long,单位ms;
3.fixedDelayString:与fixedDelay含义一样,只是参数类型变为String;
4.fixedRate:表示按一定的频率执行任务,参数类型为long,单位ms;
5.fixedRateString: 与fixedRate的含义一样,只是将参数类型变为String;
6.initialDelay:表示延迟多久再第一次执行任务,参数类型为long,单位ms;
7.initialDelayString:与initialDelay的含义一样,只是将参数类型变为String;
8.zone:时区,默认为当前时区,一般没有用到。

Cron表达式范例:

每隔5秒执行一次:*/5 * * * * ?
每隔1分钟执行一次:0 */1 * * * ?
每天23点执行一次:0 0 23 * * ?
每天凌晨1点执行一次:0 0 1 * * ?
每月1号凌晨1点执行一次:0 0 1 1 * ?
每月最后一天23点执行一次:0 0 23 L * ?
每周星期天凌晨1点实行一次:0 0 1 ? * L
在26分、29分、33分执行一次:0 26,29,33 * * * ?
每天的0点、13点、18点、21点都执行一次:0 0 0,13,18,21 * * ?

其实不会Cron表达式也不用担心,网上有好多在线Cron生成器,我们完全可以通过在线生成器生成符合要求的cron,也很方便。

(完)


参考文章
SpringBoot定时任务及Cron表达式详解

发布了31 篇原创文章 · 获赞 24 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/hengliang_/article/details/80857203
今日推荐