import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 用作定时任务的类
*/
@Configuration
@EnableScheduling
public class TimerTaskUtil {
/**
* 用作每两分钟执行一次的方法调度
*/
@Scheduled(cron = "0 0/2 * * * ?") //每两分钟执行一次
public void timerTask1(){
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
System.out.println("任务调度开始,每两分钟执行一次调度……现在的时间: " +time);
}
/**
* 用作每天凌晨推迟5分钟执行一次的方法调度
*/
@Scheduled(cron = "0 5 0 * * ?") //每天0:05:00执行,凌晨推迟5分钟执行
public void timerTask2(){
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
System.out.println("任务调度开始,每天凌晨推迟5分钟执行一次调度……现在的时间: " + time);
}
}