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)
│ │ │ │ │ │
* * * * * *
原文地址:https://blog.csdn.net/2301_79526467/article/details/140270181
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!