0

I have several scheduled jobs in Spring Boot that are defined using @Scheduled(…). I am using these scheduled jobs to test APIs. I have a ugly solution that allows me to turn enable/disable these scheduled jobs, but I was wondering if there was a more elegant solution. From my searching, I couldn't find a up-to-date, relevant solution.

Here is basically what I am working with:

@RestController
@RequestMapping("something")
public class MyController {
    ...
    @GetMapping("/map1")
    public ResponseEntity<Object> service1(...) { ... }

    @GetMapping("/map2")
    public ResponseEntity<Object> service2(...) { ... }
}

I have all the scheduled jobs located in a central location. I have implement two ways to control their execution: switch to enable/disable all scheduled tasks and one to enable/disable individual scheduled jobs.

@Component
@ConditionalOnProperty(value="jobs.status.all")
public class ScheduledJobs {

    @Autowired
    MyController mc;

    @Autowired
    Environment env;

    @Scheduled(...)
    public void job1() {
        if(Boolean.parseBoolean(env.getProperty("jobs.status.one", "false")) {
            mc.service1();
        }
    }

    @Scheduled(...)
    public void job2() {
        if(Boolean.parseBoolean(env.getProperty("jobs.status.two", "false") {
            mc.service2();
        }
    }
}

This allows me to controls scheduled jobs from a properties file(all or individual ones). My intention is remove the execution of job1() and job2() because that is ugly and inefficient.

What would be a more elegant solution to enable and disable scheduled jobs?

EDIT: I was looking for a more elegant solution of enabling/disabling individually scheduled jobs. In case where I would want to enable some jobs while disabling others.

user3303411
  • 57
  • 1
  • 7
  • No solution from this works for you https://www.baeldung.com/spring-scheduled-enabled-conditionally? – tkruse Sep 07 '19 at 00:22
  • Possible duplicate of [How to conditionally enable or disable scheduled jobs in Spring?](https://stackoverflow.com/questions/18406713/how-to-conditionally-enable-or-disable-scheduled-jobs-in-spring) – tkruse Sep 07 '19 at 00:23
  • @tkruse I don't if you bother looking at those solutions, but you can clearly see that are basically what I have already implemented. They all use flags to check. The second solutions is an even more hideous version of mine, but done in Spring. – user3303411 Sep 07 '19 at 01:01
  • It helps to know all the alternatives that are not valid answers to you, there is nlittle value in others trying to supply the same solutions as in other answers. "More elegant" is very subjective, and if not closing this question as duplicate, it could as well be closed as too broad. Maybe you can replace "more elegant" with a more specific explanation of what you dislike about the solution you currently use. – tkruse Sep 08 '19 at 02:09
  • Also related: https://stackoverflow.com/questions/55246497, https://stackoverflow.com/questions/40684903, https://stackoverflow.com/questions/28782694 – tkruse Sep 08 '19 at 02:12
  • I thought I had made it clear "enabling/disabling individually scheduled jobs". My apologies if it was not clear. My initial thought was to implement it with @ConditionalOnProperty, but I was wary if such a implementation would cause too much overhead as each task would would have it's own bean. In my case where I had about 60 different scheduled tasks. – user3303411 Sep 09 '19 at 19:35

0 Answers0