Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring scheduling task - run only once

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 service method starts at 8pm. I'm familiar with @Scheduled annotation and I'm not sure how to write cron expression not to run periodically. This one @Scheduled(cron = "0 0 20 * * ?") fires every day at 8pm.
Any suggestions?

like image 315
shx Avatar asked May 20 '15 10:05

shx


People also ask

What is @scheduled in spring?

The @EnableScheduling annotation is used to enable the scheduler for your application. This annotation should be added into the main Spring Boot application class file. @SpringBootApplication @EnableScheduling public class DemoApplication { public static void main(String[] args) { SpringApplication.

What is ThreadPoolTaskScheduler in Java?

ThreadPoolTaskScheduler ThreadPoolTaskScheduler is well suited for internal thread management, as it delegates tasks to the ScheduledExecutorService and implements the TaskExecutor interface – so that single instance of it is able to handle asynchronous potential executions as well as the @Scheduled annotation.


1 Answers

Dead man's solution:

@Scheduled(initialDelay = 1000 * 30, fixedDelay=Long.MAX_VALUE)

You will be dead before it fires again.

like image 112
Rick Ahlander Avatar answered Sep 29 '22 10:09

Rick Ahlander