天天看点

spring boot中使用Schedule做定时任务

最近看了EasySchedule就想着将原来的任务处理模式改下,也改成定时任务

一下是解决办法

@EnableScheduling
@SpringBootApplication
public class StorageStarterApplication
{
    public static void main(String[] args) {
        SpringApplication.run(StorageStarterApplication.class, args);
    }
}
           

(1) 在启动时添加@EnableScheduling注解

@Component
public class OnlineQueryHandle{
    @Autowired
    private ExecutorService executorService;
    @Scheduled(fixedDelay = 1000*10)
    public void work() {
        executorService.execute(new Runnable() {
            public void run() {
                System.out.println("Asynchronous task");
            }
        });
    }
}
           

(2) 启动定时处理work

executorService.execute(new Runnable() {
                public void run() {
                    System.out.println("Asynchronous task");
                }
            });
           

(3) 启动执行job

继续阅读