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)
This scheduled method will then call a web-service with the given parameter and keep on running until a specific response ("Success") is received.
Once the specific response is received, I want the scheduled method to stop running until it is called again from the front end.
I am not sure how to start and stop the execution of the scheduled method.
I have this currently:
@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");
}
}
}
Here is my controller through which the scheduled method is to be turned on:
@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");
}
}
Is my approach correct? How can I achieve this functionality using springboot and schedulers?
AtomicBoolean
that specifies if the code should be executed or not by using awhile
loop on it? – Verger