Spring boot定时任务
导入jar包
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
定时任务初体验
(正常情况下不指定年)
package com.example.springboot_5_task.task;
import org.joda.time.DateTime;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class TaskTest {
@Scheduled(cron = "0/10 * * * * ? ")
public void testPrint(){
DateTime dateTime = new DateTime();
String dateStr = dateTime.plusDays(1).toString("yyyy-MM-dd");
System.out.println("dateStr = " + dateStr);
}
}
按照指定格式输出
输出错误,修改后
主程序添加开启定时任务注解
package com.atguigu.task;
import org.joda.time.DateTime;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class TaskTest {
@Scheduled(cron = "30 30 10 26 7 ? ")
public void test01(){
DateTime dateTime = new DateTime();
String dateStr = dateTime.plusDays(1).toString("yyyy-MM-dd hh-mm-ss");
System.out.println("dateStr = " + dateStr);
}
}
主程序:
package com.example.springboot_5_task;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
//开启定时任务
@EnableScheduling
public class Springboot5TaskApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot5TaskApplication.class, args);
}
}
Linux
检查是否启用虚拟化
任务管理器 -> 性能 -> cpu