Springboot定时任务

1,添加依赖

        
        
            org.springframework.boot
            spring-boot-starter-actuator
            1.5.13.RELEASE
        

2,添加注解到Springbootapplication上

@EnableScheduling

3,编写定时任务
@Component
public class TaskCheck {

private static final Logger log = LoggerFactory.getLogger(TaskCheck.class);
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

@Scheduled(cron = "0 0 0 * * ?")
//cron表达式,每晚12点执行,按需更改
public void reportCurrentTime() {
    log.info("The time is now {}", dateFormat.format(new Date()));
    //代码块
}

}

你可能感兴趣的:(JavaEE)