You can not. When you set the fixedDelay
attribute to -1
or attempt use @Scheduled
without specifying a valid value for any of its attributes, Spring will complain that no attribute is set:
Exactly one of the 'cron'
, 'fixedDelay(String)'
, or 'fixedRate(String)'
attributes is required
You can verify this behavior by going through the source code of ScheduledAnnotationBeanPostProcessor#processScheduled
.
It contains logic like:
boolean processScheduled = false;
// ...
if (fixedRate >= 0) {
Assert.isTrue(!processedSchedule, errorMessage);
processedSchedule = true;
this.registrar.addFixedRateTask(new IntervalTask(runnable, fixedRate, initialDelay));
}
// ...
Assert.isTrue(processedSchedule, errorMessage);
Take a look at this SO post for some options for conditionally disabling @Scheduled
.
long
) values forinitialDelay
andfixedDelay
so they are never actually reached. This may work in practice but just seems a bit wrong to have to resort to this and a shame that Spring didn't provide a "never" option. – Fridafriday