Support for special cron value of - (as defined in Scheduled#CRON_DISABLED) was introduced in #21397 however this is limited to use of @Scheduled.
For scheduled jobs registered via SchedulingConfigurer this is not supported which means there's no way to disable scheduled job by supplying the cron value.
This can be demonstrated using a simple Spring Boot based sample application:
@SpringBootApplication
@EnableScheduling
public class SampleApplication implements SchedulingConfigurer {
private static final Logger logger = LoggerFactory.getLogger(SampleApplication.class);
public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}
@Scheduled(cron = "${ticker1.cron:* * * * * ?}")
public void ticker1() {
logger.info("ticker1");
}
public void ticker2() {
logger.info("ticker2");
}
@Value("${ticker2.cron:* * * * * ?}")
private String ticker2Cron;
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addCronTask(this::ticker2, this.ticker2Cron);
}
}
This works equally well for all cases, except for - which will blow up with java.lang.IllegalArgumentException: Cron expression must consist of 6 fields (found 1 in "-") when supplied for ticker2.cron.
Support for special cron value of
-(as defined inScheduled#CRON_DISABLED) was introduced in #21397 however this is limited to use of@Scheduled.For scheduled jobs registered via
SchedulingConfigurerthis is not supported which means there's no way to disable scheduled job by supplying the cron value.This can be demonstrated using a simple Spring Boot based sample application:
This works equally well for all cases, except for
-which will blow up withjava.lang.IllegalArgumentException: Cron expression must consist of 6 fields (found 1 in "-")when supplied forticker2.cron.