Spring Boot @EnableScheduling conditionally

前端 未结 4 1751
慢半拍i
慢半拍i 2021-02-20 03:53

Is there a way to make @EnableScheduling conditional based on an application property? also is it possible to disable controllers based on properties too?

What I\'m tryi

相关标签:
4条回答
  • 2021-02-20 04:17

    I ended up creating a separate @Configuration class for scheduling and used the @ConditionalOnProperty annotation to toggle scheduling

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

    in my application.yml file I then added

    scheduling:
      enabled: true
    
    0 讨论(0)
  • 2021-02-20 04:19

    You can annotate a bean injection like this:

    @Bean
    @ConditionalOnProperty(prefix = "your.property", name = "yes", matchIfMissing = true)
    public void doSomeBackendJob() {
           /* job implementation here */
    }
    

    Bonus: Since you want to run different things in different machines, i.e., you will deploy the same app with different configurations, you could use spring profiles, if that's the case you can annotate a class or method like this:

    @Component
    @Profile({ Constants.SPRING_PROFILE_PRODUCTION, Constants.SPRING_PROFILE_TEST })
    public class TheClass{...}
    
    0 讨论(0)
  • 2021-02-20 04:24

    I Solved this, here is what I did for future reference:

    • Removed @EnableScheduling annotation from my app
    • Added a new configuration class and conditional to enable/disable scheduling based on an application property

    -

     @Configuration
     public class Scheduler {
    
        @Conditional(SchedulerCondition.class)
        @Bean(name = TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)
        @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
        public ScheduledAnnotationBeanPostProcessor scheduledAnnotationProcessor() {
            return new ScheduledAnnotationBeanPostProcessor();
        }
    }
    

    And the conditional class

    public class SchedulerCondition implements Condition {
        @Override
        public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
            return Boolean.valueOf(context.getEnvironment().getProperty("com.myapp.config.scheduler.enabled"));
        }
    
    }
    

    Also, to disable web server on the backend server, just add the following to the application.properties file:

    spring.main.web_environment=false
    
    0 讨论(0)
  • 2021-02-20 04:29
    1. I think scheduler is not configuration.
    2. No need to set prefix in @ConditionalOnProperty
    3. No need to create scheduler over @Bean in configuration class.

    My variant:

    @Component
    @EnableScheduling
    @ConditionalOnProperty(value = "scheduler.enabled", havingValue = "true")
    public class MyScheduler {
    
        @Scheduled(...)
        public void doWork() {
            ...
        }
    }
    
    
    0 讨论(0)
提交回复
热议问题