SpringBoot - 实现定时任务

1.在启动类,加入@EnableScheduling 注解

@SpringBootApplication
@EnableScheduling
public class SpringbootScheduleApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootScheduleApplication.class, args);
    }

}

2. @Scheduled 注解实现定时任务

@Component
public class TaskService {

    @Scheduled(cron = "xxxxxx")
    public void task() {
        System.out.println("Thread Name : "
                    + Thread.currentThread().getName() + "  i am a task : date ->  "
                    + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }
}

 

posted on 2022-02-15 09:29  每天积极向上  阅读(42)  评论(0编辑  收藏  举报

导航