Spring Boot @EnableScheduling conditionally

Is there a way to make @EnableScheduling conditional based on application property? can you also disable property based controllers?

What I'm trying to achieve is to have the same spring boot application that is used to serve web requests (but not run scheduled tasks on one computer), and also install the same application on server servers to run only scheduled tasks.

My application is as follows

@SpringBootApplication @EnableScheduling @EnableTransactionManagement public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } } 

And an example of a scheduled task is as follows

 @Component public class MyTask { @Scheduled(fixedRate = 60000) public void doSomeBackendJob() { /* job implementation here */ } } 
+10
source share
4 answers

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

  • Removed @EnableScheduling annotation from my application
  • Added a new configuration class and conditional for enabling / disabling scheduling based on 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 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")); } } 

Alternatively, to disable the web server on the backend server, simply add the following to the application.properties file:

 spring.main.web_environment=false 
+8
source

You can annotate the bean injection as follows:

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

Bonus: since you want to run different things on different computers, that is, you will deploy the same application with different configurations, you can use spring profiles if in this case you can annotate a class or method as follows:

 @Component @Profile({ Constants.SPRING_PROFILE_PRODUCTION, Constants.SPRING_PROFILE_TEST }) public class TheClass{...} 
+4
source

In the end, I created a separate @Configuration class for planning and used the @ConditionalOnProperty annotation to switch planning

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

in my application.yml file I added

 scheduling: enabled: true 
+1
source
  1. I think the scheduler is not a configuration.
  2. No need to install prefix in @ConditionalOnProperty
  3. There is no need to create a scheduler over @Bean in the configuration class.

My version:

 @Component @EnableScheduling @ConditionalOnProperty(value = "scheduler.enabled", havingValue = "true") public class MyScheduler { @Scheduled(...) public void doWork() { ... } } 
0
source

All Articles