我正在尝试使用ScheduledThreadPoolExecutor's scheduleAtFixedRate
每5分钟运行一次任务,但它只是第一次运行,请让我知道我在这里做错了什么。感谢。
@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);
}
}
MyController构造函数调用startScheduler()
,但根据调度程序,它应该每5分钟运行一次getOtherResponse