How to conditionally enable or disable scheduled jobs in Spring?

前端 未结 11 1415
轻奢々
轻奢々 2020-11-28 07:26

I am defining scheduled jobs with cron style patterns in Spring, using the @Scheduled annotation.

The cron pattern is stored in a config properties file

11条回答
  •  星月不相逢
    2020-11-28 07:51

    If you are looking to toggle @EnableScheduling from a property you can do this in Spring Boot by moving the @EnableScheduling annotation to a configuration class and use @ConditionalOnProperty as follows:

    @Configuration
    @EnableScheduling
    @ConditionalOnProperty(prefix = "com.example.scheduling", name="enabled", havingValue="true", matchIfMissing = true)
    public class SchedulingConfiguration {
    
    }
    

    This will disable scheduling for the application. This may be useful in a situation where you want to be able to run the application once or scheduled depending on how it's being started.

    From wilkinsona's comment on here: https://github.com/spring-projects/spring-boot/issues/12682

提交回复
热议问题