I am trying to run the task for every 5 minutes using ScheduledThreadPoolExecutor's scheduleAtFixedRate
but it is running only for the first time, please let me know what I am doing wrong here. Thanks.
@RestController
@RequestMapping("/app")
public class MyRestController {
private RequestValidator validator;
final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(5);
private MyOtherServiceResponse myOtherServiceResponse;
@Autowired
public MyRestController(RequestValidator validator, MyOtherServiceResponse myOtherServiceResponse){
this.validator = validator;
this.myOtherServiceResponse = myOtherServiceResponse;
startScheduler();// calling scheduler for the first time to start
}
@RequestMapping( value = "/getDetails/{prdId}", method= RequestMethod.GET, produces={"application/json"})
public ResponseEntity<> getProductInfo(@PathVariable(value="prdId") String prdId) {
long endTime = 0;
long startTime = 0;
startTime = System.currentTimeMillis();
if((validator.validateRequest(prdId))== null) {
ServiceRequest serviceRequest = validator.getServiceRequest();
endTime = System.currentTimeMillis() - startTime;
//getThisServiceResponse(serviceRequest, prdId);
System.out.println("Service final Time -------------- " + endTime + " prdId : " + prdId);
return new ResponseEntity<>(HttpStatus.OK);
}
else {
errorResponse = validator.validateRequest(prdId);
return new ResponseEntity<>(errorResponse, HttpStatus.OK);
}
}
public void startScheduler(){
executor.scheduleAtFixedRate(() -> myOtherServiceResponse.getOtherResponse(validator.getServiceRequest(), validator.PRDID), 0, 5, TimeUnit.MINUTES);
}
}
The startScheduler()
is called by MyController Constructor, but as per the scheduler it should run getOtherResponse
for every 5 minutes