定时任务 @Scheduled cron、fixedRate、fixedDelay、fixedDelayString、fixedRateString

本文详细介绍了如何在Spring框架中使用cron表达式实现定时任务,区分了fixedDelay与fixedRate的区别,以及如何通过配置文件动态设置定时间隔。涵盖了定时任务的执行逻辑和配置方法。

cron 可以设置每隔周期时间执行,也可以指定在某个时间执行; 每隔指定时间执行任务,首先判断上次任务是否已经执行结束,若是已经执行结束,则执行此次任务,若是没有执行完成,则在下一次指定时间再次重复以上步骤。
例如:每隔3秒执行定时任务,0秒开始第一次执行,在第3秒的时候判断上次任务是否已经执行结束,若是已经结束,则执行此次任务,若是没有结束则在第6秒的时候再次检查任务是否执行完成...........

fixeddelay    上一次任务结束后等待指定时间执行下次任务;例如:每隔3秒执行定时任务,0秒开始第一次执行,任务执行花费5秒,则第二次任务执行开始时间是第 0+5+3=8 秒

fixedrate    每隔指定时间执行任务,若是执行任务的时间超过定时的时间,则每次上一次任务执行完毕后下一次任务立刻执行,否则上次任务开始每隔指定时间执行任务。    例如:每隔3秒执行定时任务,0秒开始第一次执行,
若是任务执行了4秒,则第二次任务执行开始时间是第4秒,若是任务执行了6秒,则第二次任务执行开始时间是第6秒;若是任务执行了2秒,则第二次任务执行开始时间是第3秒

fixedDelayString    同fixeddelay,值可以从配置文件获得

fixedRateString      同fixedrate,值可以从配置文件获得

 

    @Scheduled(cron ="0/1 * * * * ? ")
    public void testCron() {
        log.info("开始 testCron1 test ---------");
    }


    @Scheduled(fixedRate = 4000)
    public void testFixedRate() {
        log.info("开始 fixedRate test ---------");
    }

    @Scheduled(fixedDelay = 4000L)
    public void testFixedDelay() {
        log.info("开始 fixedDelay test ---------");
    }

    /**
     * @Description:  fixedDelayString 可以从配置文件取值
     */
    @Scheduled(fixedDelayString = "${fixedDelayString}")
    public void TestFixedDelayString() {
        log.info("开始 fixedDelayString test ---------");
    }

    /**
     * @Description:  fixedRateString 可以从配置文件取值
     */
    @Scheduled(fixedRateString = "${fixedRateString}")
    public void TestFixedRateString() {
        log.info("开始 fixedRateString test ---------");
    }

 

当 Java 中使用 `@Scheduled(fixedRate = 60000)` 自定义线程池不起作用时,可以从以下几个方面排查并解决: ### 配置类问题 确保配置类正确实现了 `SchedulingConfigurer` 接口,并且使用了 `@Configuration` 和 `@EnableScheduling` 注解。以下是正确的配置类示例: ```java import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.springframework.scheduling.config.ScheduledTaskRegistrar; import java.util.concurrent.Executor; import java.util.concurrent.Executors; @Configuration @EnableScheduling public class SchedulingConfig implements SchedulingConfigurer { @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { Executor executor = Executors.newScheduledThreadPool(5); taskRegistrar.setScheduler(executor); } } ``` ### 依赖问题 检查项目的依赖是否正确,确保 Spring 相关的依赖已经正确添加到项目中。如果使用 Maven,可以在 `pom.xml` 中添加以下依赖: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-scheduling</artifactId> </dependency> </dependencies> ``` ### 线程池配置问题 确保线程池的配置没有被其他配置覆盖。有时候,其他配置可能会影响到自定义线程池的使用。可以检查是否有其他地方对定时任务的线程池进行了配置。 ### 定时任务类问题 确保定时任务类被 Spring 容器管理,使用 `@Component` 或其他 Spring 组件注解。以下是定时任务类的示例: ```java import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class ScheduledTasks { @Scheduled(fixedRate = 60000) public void performTask() { System.out.println("定时任务开始执行,当前线程: " + Thread.currentThread().getName()); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("定时任务执行结束,当前线程: " + Thread.currentThread().getName()); } } ``` ### 应用启动问题 确保 Spring 应用已经正确启动,并且 `@EnableScheduling` 注解已经启用。以下是应用启动类的示例: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ```
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值