定时任务:
任务需要定时更新,每天的凌晨备份下数据,每个月清空一下日志,或者定义下过期时间。每个工程或者是任务在规定的频度,间隔内运行。
知识点:
@Scheduled注解: https://blog.csdn.net/wal1314520/article/details/80758539
代码如下:
package com.example.exceltest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
// 开启定时任务
@EnableScheduling
public class ExceltestApplication {
public static void main(String[] args) {
SpringApplication.run(ExceltestApplication.class, args);
}
}
package com.example.exceltest;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class MyTask {
// initialDelay = 1000初始化为1s,fixedDelay = 1000 每间隔1s执行一次
@Scheduled(initialDelay = 1000,fixedDelay = 1000)
public void firstTask(){
System.out.println(new Date() + ":这是第一个定时任务");
}
// 每间隔5秒,执行一次 第二个定时任务
@Scheduled(cron = "*/5 * * * * ?")
public void secondTask() {
System.out.println(new Date() + ":这是第二个定时任务");
}
}