Skip to content

Provide support for disabling cron-based scheduled jobs registered via SchedulingConfigurer #23568

@vpavic

Description

@vpavic

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.

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions