spring 计划任务

spring 通过@Scheduled 支持多种类型的计划任务,包含cron、fixDelay、fixRate

计划任务执行类:

@Service
public class ScheduleTaskService {
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    //声明该方法是计划任务,使用fixedRate属性每隔固定时间执行
    @Scheduled(fixedRate = 5000)
    public void reportCurrentTime(){
        System.out.println("每隔五秒执行一次"+dateFormat.format(new Date()));
    }
    //使用cron属性可按照指定时间执行
    @Scheduled(cron="0 57 14 ? * *")
    public void fixTimeExcution(){
        System.out.println("在指定时间"+dateFormat.format(new Date())+"执行");
    }
}

配置类:

@Configuration
@ComponentScan("com.hong.stu.service")
@EnableScheduling
public class TaskSchedulerConfig {
}

猜你喜欢

转载自my.oschina.net/u/2262481/blog/1793269