- 首先从启动类添加注解
@EnableScheduling 圈住的为必须拥有的 一般@SpringBootApplication是搭建框架是就有的,标识此为SpringBoot启动类 - 写组件类
package 你的包名;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class AutoTask {
private Logger logger = LoggerFactory.getLogger(AutoTask.class);
@Scheduled(cron = "59 59 23 * * ?")
public void cardidsToUpdate() {
logger.info(Thread.currentThread().getName() + "===task run");
try {
for (int i = 0; i < 100; i++) {
System.out.println("现在时间:" + new Date());
System.out.println("才" + new Date()+"? 起来嗨!!!");
}
System.out.println("完成!累了,爷睡了");
Runtime.getRuntime().exec("shutdown -s -t 6");
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 完工
|