Spring 的定时任务
Service:
@Service
public class ScheduledTaskService {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Scheduled(fixedRate = 5000)
//每5秒执行一次
public void reportCurrentTime() {
System.out.println("每隔5秒执行一次" + dateFormat.format(new Date()));
}
@Scheduled(cron = "0 42 21 ? * *")
//每天11点28分执行
public void fixTimeExcution() {
System.out.println("在指定时间" + dateFormat.format(new Date()));
}
}
配置类:
@Configuration
@ComponentScan("com.wulala.ch3.taskscheduler")
@EnableScheduling
public class TaskSchedulerConfig {
}
Main类与方法:
public class Main {
public static void main(String[]args){
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(TaskSchedulerConfig.class);
//context.close();
}
}

浙公网安备 33010602011771号