Spring scheduling task - run only once

shx picture shx · May 20, 2015 · Viewed 48.7k times · Source

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?

Answer

Rick Ahlander picture Rick Ahlander · Apr 23, 2019

Dead man's solution:

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

You will be dead before it fires again.