SpringBoot实战:定时任务

1. 启动定时任务需在配置类中添加@EnableScheduling注解

@SpringBootApplication
@EnableScheduling //设置定时任务需在相应模块下添加注解@EnableScheduling //并定义类实现定时任务
public class AdminWebApplication {
    public static void main(String[] args) {
        SpringApplication.run(AdminWebApplication.class, args);
    }
}

2. 创建定时任务类

@Component
public class ScheduleTasks {

    /*//设置定时任务
    @Scheduled(cron = "* * * * * *") //*d代表任意,cron表达式设置什么时候执行以下方法,此时为每一秒都要执行
    public void test() {
        System.out.println(new Date());
    }*/

    @Autowired
    private LeaseAgreementService leaseAgreementService;

    @Scheduled(cron = "0 0 0 * * *") //*d代表任意,cron表达式设置什么时候执行以下方法,此时为每天0点整执行
    public void checkLeaseStatus() {
        LambdaUpdateWrapper<LeaseAgreement> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.le(LeaseAgreement::getLeaseEndDate, new Date());
        updateWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING);
        updateWrapper.set(LeaseAgreement::getStatus, LeaseStatus.SIGNED);

        leaseAgreementService.update(updateWrapper);
    }
}

设置任务时长语法如下

  ┌───────────── second (0-59)
  │ ┌───────────── minute (0 - 59)
  │ │ ┌───────────── hour (0 - 23)
  │ │ │ ┌───────────── day of the month (1 - 31)
  │ │ │ │ ┌───────────── month (1 - 12) (or JAN-DEC)
  │ │ │ │ │ ┌───────────── day of the week (0 - 7)
  │ │ │ │ │ │          (0 or 7 is Sunday, or MON-SUN)
  │ │ │ │ │ │
  * * * * * *

相关推荐

  1. SpringBoot实战定时任务

    2024-07-15 06:22:02       21 阅读
  2. SpringBoot 实现定时任务

    2024-07-15 06:22:02       56 阅读
  3. 使用SpringBoot实现定时任务

    2024-07-15 06:22:02       40 阅读
  4. springboot定时任务

    2024-07-15 06:22:02       61 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-15 06:22:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-15 06:22:02       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-15 06:22:02       58 阅读
  4. Python语言-面向对象

    2024-07-15 06:22:02       69 阅读

热门阅读

  1. .NET 开源库技术栈汇总

    2024-07-15 06:22:02       20 阅读
  2. UDP 报文结构与注意事项全解析

    2024-07-15 06:22:02       28 阅读
  3. 深入理解Symfony框架中的数据验证机制

    2024-07-15 06:22:02       21 阅读
  4. OpenCV——实现视频图像的来回摆动的效果

    2024-07-15 06:22:02       17 阅读
  5. 【c++】VSstudio win32 应用开发

    2024-07-15 06:22:02       24 阅读
  6. 深入理解Scikit-learn:决策树与随机森林算法详解

    2024-07-15 06:22:02       22 阅读