Spring Boot @EnableScheduling conditionally

spring boot disable scheduling
spring cron disable expression
spring boot disable scheduled task in test
spring conditional on property
spring boot scheduler example mkyong
spring boot multiple scheduled tasks
spring conditionalonproperty
spring scheduler cron expression never run

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 trying to achieve is to have the same spring boot application used to serve web requests (but not run scheduled tasks on the same machine), and also install the same app on backend servers to run only scheduled tasks.

My app looks like this

@SpringBootApplication
@EnableScheduling
@EnableTransactionManagement
public class MyApp {

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

}

And a sample scheduled job looks like this

@Component
public class MyTask {

   @Scheduled(fixedRate = 60000)
   public void doSomeBackendJob() {
       /* job implementation here */
   }
}

Spring Boot @EnableScheduling conditionally, I Solved this, here is what I did for future reference: Removed @​EnableScheduling annotation from my app; Added a new configuration class  The simplest way to conditionally enable a Spring scheduled job is to use a boolean variable that we check inside the scheduled job. The variable can be annotated with @Value to make it configurable using normal Spring configuration mechanisms :

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{...}

How to conditionally enable or disable scheduled jobs in Spring , If you are looking to toggle @EnableScheduling from a property you can do this in Spring Boot by moving the @EnableScheduling annotation  To schedule job in spring boot application to run periodically, spring boot provides @EnableScheduling and @Scheduled annotations. Lets learn to use Spring boot @Scheduled annotation. Let’s say you want to run job at every 10 seconds interval. You can achieve this job scheduling in below steps: Add @EnableScheduling annotation to your spring

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

java: Spring Boot @EnableScheduling conditionally, Is there a way to make @EnableScheduling conditional based on an application property? also is it possible to disable controllers based on  In this tutorials, we are going to see how can we use TaskScheduler using Spring Boot Scheduler. And also we will have a discussion on how to use Spring @EnableScheduling and @Scheduled annotations.we can use the java based @EnableScheduling annotation in spring boot applications

  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() {
        ...
    }
}

EnableScheduling (Spring Framework 5.2.5.RELEASE API), I am defining scheduled jobs with cron style patterns in Spring, using the in Spring Boot by moving the @EnableScheduling annotation to a configuration class  Using @Conditional with @Configuration in Spring allows us to group @Bean objects that are loaded on a specific condition without resorting to using @Profile and too many @Bean/@Conditional.

Scheduling Spring Boot Tasks, @Configuration @EnableScheduling public class AppConfig { @Bean public MyTask task() { return new MyTask(); } } Alternatively, if MyTask were annotated  Spring has introduced the @Conditional annotation that allows us to define custom conditions to apply to parts of our application context. Spring Boot builds on top of that and provides some pre-defined conditions so we don’t have to implement them ourselves.

spring-projects/spring-boot, The @EnableScheduling annotation allows the Spring container to notice any @​Scheduled annotations in Spring-managed beans. Conditional Scheduling. Spring provides @EnableScheduling annotation. We can achieve scheduling of the task. @EnableScheduling is used with @Configuration in XML free bean creation. The beans which need to be scheduled must have @Scheduled annotaion.

Spring, https://docs.spring.io/spring-boot/docs/current/reference/html/common- @​EnableScheduling public class Application { public static void  Spring @Conditional annotation allows us to register components based on the outcome of one or more conditions. We will specify the conditions in @Conditional annotation. Each condition is of type Condition and implements match(). Unless all the conditions return true the component will not be eligible for registration.

Comments
  • Found this to be a lot of extra work since I need to disable/enable all jobs at once, check my answer
  • @ConditionalOnProperty will have no effect on a @Scheduled method as, like all conditions, it only affects bean registration. See the javadoc for further details: docs.spring.io/spring-framework/docs/4.3.2.RELEASE/javadoc-api/…
  • My bad and thanks for the correction, I just edited the answer :)
  • That still won't work. The job will never run as it's never going to be scheduled
  • @TimStack Don't complain about the post and then OK it in Review. You defeat the purpose for what you are complaining about.
  • @Rob I am not complaining. If I believe an answer may truly be a helpful answer I'd rather see it edited and OP taught than see the post be deleted.
  • @TimStack Expanded. :)