Spring的定时任务
先来简单看下定时任务代码和运行:
package org.example;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
// 定时任务部分
@Configuration
@EnableScheduling
public class MyTask {
// 每10s打印一次
// spring task
//cron:定时任务表达式
@Scheduled(cron = "0/10 * * * * *")
public void task1(){
System.out.println("MyTask.task1()");
}
// 每1min打印一次
@Scheduled(cron = "0 0/1 * * * *")
public void task2(){
System.out.println("MyTask.task2()");
}
}
下面这几篇重点学习一下:
Spring第38篇:定时器详解(@Scheduled & @EnableScheduling)-CSDN博客
Spring Task:实现定时任务的高效解决方案_springtask-CSDN博客