Spring scheduling task - run only once

后端 未结 5 1057
我寻月下人不归
我寻月下人不归 2020-11-29 04:42

Is it possible to schedule Spring service method only once at exactly specified time? For example, current time is 2pm but when I hit the action button I want that my servic

5条回答
  •  盖世英雄少女心
    2020-11-29 05:11

    In order to not create ScheduledExecutorService and ConcurrentTaskScheduler at every method call it is convenient to initialize TaskScheduler at service creation, e.g.

    private final TaskScheduler taskScheduler = 
                  new ConcurrentTaskScheduler(Executors.newScheduledThreadPool(10));
    

    @Async has no sense as we just schedule task and exit from method.

提交回复
热议问题