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);
}
}