Search code examples
springcronscheduled-tasks

Spring Cron scheduler “disable pattern”


My application loads some cron patterns from a properties file. I'm using the @Scheduled annotation like this:

@Scheduled(cron = "${config.cronExpression:0 0 11,23 * * *}")

Now I want to disable some tasks and the easiest solution would be to enter a cron pattern which will never run. In order to do this, I thought about using a cron expression that only executes at a specific day in the past. But unfortunately the Spring cron expressions don't allow to add a year or a date in the past.

Is there any pattern that will never run?


Solution

  • As of Spring 5.1.0 the @Scheduled annotation can accept "-" as the cron expression to disable the cron trigger.

    Per the Javadocs:

    The special value "-" indicates a disabled cron trigger, primarily meant for externally specified values resolved by a ${...} placeholder.