sprint boot計劃任務
阿新 • • 發佈:2018-12-21
@Service
public class ScheduledTaskService {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Scheduled(fixedRate = 5000)
public void reportCurrentTime() {
System.out.println("每隔5秒執行一次 " + dateFormat.format(new Date()));
}
@Scheduled (cron = "0 23 9 ? * * ")
public void fixTimeExecution() {
System.out.println("在指定時間 " + dateFormat.format(new Date()) + "執行");
}
}
@Configuration
@ComponentScan("ch3.taskscheduler")
@EnableScheduling
public class TaskSchedulerConfig {
}
public class Main {
public static void main (String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskSchedulerConfig.class);
}
}