title: How to dynamically turn on/off a scheduled method in a springboot application
body: <p>I am building a Springboot application and I want to turn on a scheduled method from the front-end. (as in I want the scheduler to run only after the method is called from the front-end)</p>
  
  <p>This scheduled method will then call a web-service with the given parameter and keep on running until a specific response ("Success") is received.</p>
  
  <p>Once the specific response is received, I want the scheduled method to stop running until it is called again from the front end.</p>
  
  <p>I am not sure how to start and stop the execution of the scheduled method.</p>
  
  <p>I have this currently:</p>
  
  <pre><code>@Component
  public class ScheduledTasks {
  
      private static final Logger LOG = LoggerFactory.getLogger(ScheduledTasks.class);
  
      private static final SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
  
      @Scheduled(fixedRate = 5000)
      public void waitForSuccess(String componentName) {
          LOG.info("Running at: " + dateFormat.format(new Date()));
          String response = MyWebService.checkStatus(componentName);
          if ("success".equalsIgnoreCase(response)) {
              LOG.info("success");
              //Stop scheduling this method
          } else {
              LOG.info("keep waiting");
          }
      }
  }
  </code></pre>
  
  <p>Here is my controller through which the scheduled method is to be turned on:</p>
  
  <pre><code>@Controller
  public class MainController {
  
      @GetMapping(/start/{componentName})
      public @ResponseBody String startExecution(@PathVariable String componentName) {
          //do some other stuff
          //start scheduling the scheduled method with the parameter 'componentName'
          System.out.println("Waiting for response");
      }
  
  }
  </code></pre>
  
  <p>Is my approach correct? How can I achieve this functionality using springboot and schedulers?</p>