基于springboot使用@Scheduled注解或实现SchedulingConfigurer类做定时调度

1.spring自带的定时器概述:

   spring自带定时器实现有两种方式:①使用xml文件配置方式②使用注解方式

2.本文详解注解方式@Schedule 和 实现SchedulingConfigurer

3.@Scheduled有三种定时任务的执行方式

  • fixedDelay
  • fixedRate
  • corn

fixedDelay:指定两次任务执行的时间间隔(毫秒),前一个任务结束与下一个任务开始的间隔

如:@Scheduled(fixedDelay = 5*1000 ),表示第一个任务执行结束,开始计时,过5秒后,开始第二次执行。

fixedRate:指定两次任务执行的时间间隔(毫秒),前一个任务开始与下一个任务开始的间隔。

如:@Scheduled(fixedRate= 5*1000 ),表示第一个任务开始执行,开始计时,过5秒后,开始第二次执行。

弊端:对于单线程任务来讲,如果执行时间大于间隔时间,会导致程序阻塞,依次类推,后面会接连发生阻塞。

corn:使用表达式执行任务(推荐使用)

cron一般是六个或七个字段,如下:

  1. Seconds (秒)
  2. Minutes(分)
  3. Hours(小时)
  4. Day-of-Month  (天)
  5. Month(月)
  6. Day-of-Week (周)
  7. Year(年)

4.在线生成corn表达式网址:http://cron.qqe2.com/

简单串行实现:

  • 在springboot启动类加上注解:@EnableScheduling
  • 在自己定义的任务类加上注解:@Component
  • 在方法上加上注解: @Scheduled
@SpringBootApplication
@EnableScheduling
public class FirePatrolApplication {

    public static void main(String[] args) {
       new SpringApplicationBuilder(FirePatrolApplication.class).run(args);
    }
}
@Component
public class ValidityRemindTask {

    private static final long SECOND = 1000;

    // cron = "0 10 0 1/1 * ?" 每天00:10执行      -----0 0 1/1 * * ?  一小时执行一次测试用
    @Scheduled(cron = "0 10 0 1/1 * ?")
    public void testTask() {
        // 根据业务写逻辑实现
    }
}

 

异步调用,开启多线程(本方法可搭配fixedRate,避免线程阻塞

  • 在自己定义的任务类加上注解:@EnableAsync   @Component
  • 在方法上加上注解: @Scheduled   @Async
@Component
@EnableAsync
public class ValidityRemindTask {

    private static final long SECOND = 1000;

    // cron = "0 10 0 1/1 * ?" 每天00:10执行      -----0 0 1/1 * * ?  一小时执行一次测试用
    @Scheduled(cron = "0 10 0 1/1 * ?")
    @Async
    public void testTask() {
        // 根据业务写逻辑实现
    }
}

SchedulingConfigurer实现使用

  • 实现SchedulingConfigurer并重写configureTasks方法
  • 在启动类必须加上@EnableScheduling
@Component
public class ValidityRemindTask implements SchedulingConfigurer {

    private static final long SECOND = 1000;

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.addCronTask(new Runnable() {
            @Override
            public void run() {
                // 根据业务逻辑具体实现
            }
        }, "0 0 1/1 * * ?"); //cron表达式
    }

}
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个使用 `@Scheduled` 注解实现定时任务的 Spring Boot 示例代码: 首先,在 Spring Boot 项目的 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> ``` 然后,在一个 Spring Boot 的 Java 中添加以下代码: ```java import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class ScheduledTasks { @Scheduled(fixedRate = 5000) // 每隔 5 秒执行一次 public void task1() { System.out.println("Task1: " + System.currentTimeMillis()); } @Scheduled(cron = "0 0/1 * * * ?") // 每分钟执行一次 public void task2() { System.out.println("Task2: " + System.currentTimeMillis()); } } ``` 上面代码中,我们使用 `@Scheduled` 注解实现两个定时任务,一个是每隔 5 秒执行一次,另一个是每分钟执行一次。 其中,`@Scheduled` 注解有两个常用的属性: - `fixedRate`:表示任务执行的间隔时间,单位为毫秒。 - `cron`:表示基于 Cron 表达式的定时任务配置,格式为 `second minute hour dayOfMonth month dayOfWeek year`。 最后,启动 Spring Boot 应用程序,即可看到定时任务在后台自动执行的输出结果。 注意:在 Spring Boot 应用程序中使用 `@Scheduled` 注解时,需要在应用程序主上添加 `@EnableScheduling` 注解,以启用定时任务功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值