springboot计划任务@EnableScheduling和@Scheduled

简介: springboot计划任务@EnableScheduling和@Scheduled

springboot计划任务@EnableScheduling和@Scheduled

@Scheduled中的参数说明

  • @Scheduled(fixedRate=2000):上一次开始执行时间点后2秒再次执行;
  • @Scheduled(fixedDelay=2000):上一次执行完毕时间点后2秒再次执行;
  • @Scheduled(initialDelay=1000, fixedDelay=2000):第一次延迟1秒执行,然后在上一次执行完毕时间点后2秒再次执行;
  • @Scheduled(cron=“* * * * * ?”):按cron规则执行。

  • service
package ch2.scheduler;
 //时间处理,时间格式化
 import java.util.Date;
 import java.text.SimpleDateFormat;
 //spring计划任务声明(针对方法声明)
 import org.springframework.scheduling.annotation.Scheduled;
 //spring组件声明
 import org.springframework.stereotype.Service;
 //组件什么
 @Service
 public class SchedulerService {
     //创建日期模板
     private static final SimpleDateFormat  dateFormat = new mpleDateFormat("HH::MM::ss");
     //每5秒钟执行一次
     @Scheduled(fixedRate = 5000)
     public void reportCurrentTime()
     {
         System.out.println("每五秒执行一次: " + dateFormat.format( new Date() ));
     }
     //按照cron属性指定执行时间:秒分时
     @Scheduled(cron = "0 34 18 ? * *")
     public void fixTimeExecution()
     {
         System.out.println("在指定的时间内执行: " + dateFormat.format( new Date()) );
     }  
 }
  • config
package ch2.scheduler;
 //自动引入包
 import org.springframework.context.annotation.ComponentScan;
 //配置类声明
 import org.springframework.context.annotation.Configuration;
 //计划任务类声明
 import org.springframework.scheduling.annotation.EnableScheduling;
 //配置类声明
 @Configuration
 //自动引入包
 @ComponentScan("ch2.scheduler")
 //通过@EnableScheduling开启对计划任务的支持
 @EnableScheduling
 public class TaskSchedulerConfig {  
 }     
  • main
package ch2.scheduler;
 //引入容器
 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 public class Main {
     public static void main(String[] args)
     {
         AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskSchedulerConfig.class);
         //SchedulerService schedulerService  = context.getBean(SchedulerService.class);  
     }
 }
  • 注意:SpringBoot使用@scheduled定时执行任务的时候是在一个单线程中,如果有多个任务,其中一个任务执行时间过长,则有可能会导致其他后续任务被阻塞直到该任务执行完成。也就是会造成一些任务无法定时执行的错觉
  • 测试
@Scheduled(cron = "0/1 * * * * ? ")
public void deleteFile() throws InterruptedException {
    log.info("111delete success, time:" + new Date().toString());
    Thread.sleep(1000 * 5);//模拟长时间执行,比如IO操作,http请求
}
@Scheduled(cron = "0/1 * * * * ? ")
public void syncFile() {
    log.info("222sync success, time:" + new Date().toString());
}

syncFile被阻塞了,直达deleteFile执行完它才执行了

而且从日志信息中也可以看出@Scheduled是使用了一个线程池中的一个单线程来执行所有任务的。

把Thread.sleep(1000*5)注释了,再次测试

  • 解决
  1. 将@Scheduled注释的方法内部改成异步执行
//构建一个合理的线程池也是一个关键,否则提交的任务也会在自己构建的线程池中阻塞
ExecutorService service = Executors.newFixedThreadPool(5);
@Scheduled(cron = "0/1 * * * * ? ")
public void deleteFile() {
    service.execute(() -> {
        log.info("111delete success, time:" + new Date().toString());
        try {
            Thread.sleep(1000 * 5);//改成异步执行后,就算你再耗时也不会印象到后续任务的定时调度了
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    });
}
@Scheduled(cron = "0/1 * * * * ? ")
public void syncFile() {
    service.execute(()->{
        log.info("222sync success, time:" + new Date().toString());
    });
} 
  1. 把Scheduled配置成成多线程执行
@Configuration
public class ScheduleConfig implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        //当然了,这里设置的线程池是corePoolSize也是很关键了,自己根据业务需求设定
        taskRegistrar.setScheduler(Executors.newScheduledThreadPool(5));
        /**为什么这么说呢?
        假设你有4个任务需要每隔1秒执行,而其中三个都是比较耗时的操作可能需要10多秒,而你上面的语句是这样写的:
        taskRegistrar.setScheduler(Executors.newScheduledThreadPool(3));
        那么仍然可能导致最后一个任务被阻塞不能定时执行
        **/
    }
}

常用Cron表达式( 秒/分/时/日/月/周/年 )

  • 0 0 10,14,16 * * ? 每天上午10点,下午2点,4点
  • 0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
  • 0 0 12 ? * WED 表示每个星期三中午12点
  • “0 0 12 * * ?” 每天中午12点触发
  • “0 * 14 * * ?” 在每天下午2点到下午2:59期间的每1分钟触发
  • “0 0/5 14 * * ?” 在每天下午2点到下午2:55期间的每5分钟触发
  • “0 0/5 14,18 * * ?” 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
  • “0 0-5 14 * * ?” 在每天下午2点到下午2:05期间的每1分钟触发
  • “0 10,44 14 ? 3 WED” 每年三月的星期三的下午2:10和2:44触发
  • “0 15 10 ? * MON-FRI” 周一至周五的上午10:15触发
  • “0 15 10 15 * ?” 每月15日上午10:15触发
  • “0 15 10 L * ?” 每月最后一日的上午10:15触发
  • “0 15 10 ? * 6L” 每月的最后一个星期五上午10:15触发
  • “0 15 10 ? * 6L 2002-2005” 2002年至2005年的每月的最后一个星期五上午10:15触发
  • “0 15 10 ? * 6#3” 每月的第三个星期五上午10:15触发
  • “0 15 10 ? * *” 每天上午10:15触发
  • “0 15 10 * * ?” 每天上午10:15触发
  • “0 15 10 * * ? *” 每天上午10:15触发
  • “0 15 10 * * ? 2005” 2005年的每天上午10:15触发


相关文章
|
Java Spring
动态控制 Spring Boot 中的 @Scheduled 定时任务
Spring Boot 中的 @Scheduled 注解为定时任务提供了一种很简单的实现,只需要在注解中加上一些属性,例如 fixedRate、fixedDelay、cron(最常用)等等,并且在启动类上面加上 @EnableScheduling 注解,就可以启动一个定时任务了。 但是在某些情况下,并没有这么简单,例如项目部署上线之后,我们可能会修改定时任务的执行时间,并且停止、重启定时任务等,因为定时任务是直接写死在程序中的,修改起来不是非常的方便。所以,简单记录一下自己的一些解决方案,仅供参考。
1699 0
|
2天前
|
Java 调度 Maven
Springboot实战篇--Springboot框架通过@Scheduled实现定时任务
Spring Boot的Scheduled定时任务无需额外Maven依赖,通过`@EnableScheduling`开启。任务调度有两种方式:fixedRate和fixedDelay,前者任务结束后立即按设定间隔执行,后者在任务完成后等待设定时间再执行。更灵活的是cron表达式,例如`0 0 3 * * ?`表示每天3点执行。实现定时任务时,需注意默认单线程执行可能导致的任务交错,可通过自定义线程池解决。
|
2天前
|
Java Spring
springboot自带的@Scheduled注解开启定时任务
springboot自带的@Scheduled注解开启定时任务
|
5月前
|
XML Java 调度
SpringBoot中定时任务入门(@Scheduled )详解
SpringBoot中定时任务入门(@Scheduled )详解
73 0
|
11月前
|
Java
springboot工程如何设置定时任务详解(@Scheduled)以及cron表达式
springboot工程如何设置定时任务详解(@Scheduled)以及cron表达式
|
缓存 NoSQL Java
SpringBoot定时任务@Scheduled的多线程使用
@Scheduled是Spring框架中的一个注解,它可以用于配置定时任务,使得方法可以按照规定的时间间隔定时执行。在使用该注解时,我们可以指定任务的执行时间、循环周期、并发数等参数,从而实现定时任务的功能。在Spring Boot中,@Scheduled注解可以直接应用于方法上。
365 0
|
Java Maven Spring
SpringBoot整合定时任务----Scheduled注解实现(一个注解全解决)
一、使用场景 定时任务在开发中还是比较常见的,比如:定时发送邮件,定时发送信息,定时更新资源,定时更新数据等等…
SpringBoot整合定时任务----Scheduled注解实现(一个注解全解决)
|
Java 调度 Spring
SpringBoot从入门到精通(三十五)使用@Scheduled实现多线程定时任务
默认情况下,Spring Boot定时任务是按单线程方式执行的,也就是说,如果同一时刻有两个定时任务需要执行,那么只能在一个定时任务完成之后再执行下一个。如果只有一个定时任务,这样做肯定没问题;当定时任务增多时,如果一个任务被阻塞,则会导致其他任务无法正常执行。要解决这个问题,需要配置任务调度线程池。
SpringBoot从入门到精通(三十五)使用@Scheduled实现多线程定时任务
|
监控 Java Spring
Spring Boot中使用@Scheduled创建定时任务
Spring Boot中使用@Scheduled创建定时任务
87 0