https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html


@SpringBootApplication
--
@EnableScheduling
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class);
    }
}

When Scheduling is enabled some times we need to deploy our application on multiple server, but want to run tasks only on one server, so we need a property in application.properties file to specify if task should run

https://stackoverflow.com/questions/49533543/spring-and-scheduled-tasks-on-multiple-instances https://jira.spring.io/browse/SPR-16666

Comment From: wilkinsona

Thanks for the suggestion, but we don't have any auto-configuration for enabling scheduling so I don't think it's appropriate to provide a property to turn it off.

If you want @EnableScheduling to be controllable via a property, I would recommend that you add it to your own configuration class that is annotated with @ConditionalOnProperty. Something like this:

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

}

This will enable scheduling unless com.example.scheduling.enabled has been set to false.

Comment From: raderio

Yes, we can check this property on every task and decide to run the task or no. But I think a solution out the box will be better.

Comment From: wilkinsona

It doesn't need to be checked on every task. It's only needed in a single place as I showed in my example above.

But I think a solution out the box will be better.

Given that there's no out-of-the-box support for enabling scheduling automatically, I don't see how we can provide out-of-the-box support for disabling it as we have no way of knowing how it was enabled.

Comment From: raderio

It doesn't need to be checked on every task. It's only needed in a single place as I showed in my example above.

I mean that yes we can solve this in another way

Given that there's no out-of-the-box support for enabling scheduling automatically, I don't see how we can provide out-of-the-box support for disabling it as we have no way of knowing how it was enabled.

Well, you know about @EnableScheduling

Comment From: snicoll

@raderio ignoring the fact that your use case seems a bit fishy to me (there are scheduling solutions that deal with the fact there are multiples instances at play), I think Andy already explained quite well why we can't add this feature.

Let me try yet another way. We can't add a property because we don't have support for scheduling. You have to add @EnableScheduling currently, which is a framework feature, and we can't "undo" that. There has been discussion to offer some scheduling support in the future in Spring Boot and I am quite convinced that, if we go down that route, we wouldn't add such property anyway.

Comment From: chomu513

can we create a api class and do enabling and disabling of scheduler whose value are present in application.properties as false?

Comment From: wilkinsona

@chomu513 Please ask questions on Stack Overflow or Gitter.

Comment From: chomu513

@wilkinsona

i have asked din't get the reply

Comment From: wilkinsona

@chomu513 As I said on Gitter, it isn't reasonable to expect a reply minutes after asking your question. Please be patient and respect the time of those that are trying to help you.