我正在尝试为100个任务运行执行程序。当前,它正在处理10个任务,但是当我发送包含11个任务的请求时,其中1个将失败(超过10个将失败)。当我调试它时,我意识到对11任务的调用根本没有被调用,而且我还没有找到问题所在。如果代码有任何问题,请通知我。
控制器:
@RequestMapping(value = "/loaderquote", method = RequestMethod.POST)
public ResponseEntity<Object> getQuoteForLoader(@RequestBody List<JSONObject> getQuoteJson,
@RequestParam("productcode") String productCode, @RequestHeader("Authorization") String authorization) {
logger.info("Entered into getQuoteForLoader()");
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("Access-Control-Allow-Origin", "*");
try {
return motorService.getQuoteForLoader(getQuoteJson, productCode, authorization);
} catch (Exception e) {
logger.error("Failed getQuoteForLoader()", e);
return new ResponseEntity<Object>(e.getMessage(), responseHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
服务:
@Scheduled(fixedRate = 3600000)
public ResponseEntity<Object> getQuoteForLoader(List<JSONObject> getQuoteJson, String productCode,
String authorization) throws ParseException, IOException, Exception {
logger.info("Entered into getQuoteForLoader()");
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("Access-Control-Allow-Origin", "*");
CompletableFuture<JSONArray> future = null;
JSONArray responseArray = new JSONArray();
try {
executor = Executors.newCachedThreadPool();
for (int i = 0; i < getQuoteJson.size(); i++) {
Thread.sleep(2000);
JSONObject jsonObject = (JSONObject) getQuoteJson.get(i);
future = CompletableFuture.supplyAsync(() -> {
// ResponseEntity<Object> responseEntity =
// motorValidation.issuePolicyValidation(jsonObject,
// authorization);
// if (responseEntity.getStatusCode() == HttpStatus.OK &&
// responseEntity.getBody() != null) {
JSONObject response = asynCallService.getQuoteAsyncService(jsonObject, productCode, authorization);
responseArray.add(response);
// }
return responseArray;
}, executor);
}
return new ResponseEntity<Object>(future.get(), responseHeaders, HttpStatus.OK);
} catch (Exception e) {
logger.error("Failed getQuoteForLoader()", e);
// return new ResponseEntity<Object>(e.getMessage(),
// responseHeaders,
// HttpStatus.INTERNAL_SERVER_ERROR);
throw e;
} finally {
executor.shutdown();
try {
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
} catch (Exception e) {
logger.error("Not terminated properly in getQuoteForLoader().", e);
}
}
}
答案 0 :(得分:0)
您可以按以下方式更改代码,
List<CompletableFuture<JSONObject>> listOfCompletableFutures = new ArrayList<>();
JSONArray responseArray = new JSONArray();
try {
executor = Executors.newCachedThreadPool();
for (int i = 0; i < getQuoteJson.size(); i++) {
JSONObject jsonObject = (JSONObject) getQuoteJson.get(i);
CompletableFuture<JSONObject> future = CompletableFuture.supplyAsync(() -> {
// ResponseEntity<Object> responseEntity =
// motorValidation.issuePolicyValidation(jsonObject,
// authorization);
// if (responseEntity.getStatusCode() == HttpStatus.OK &&
// responseEntity.getBody() != null) {
JSONObject response = asynCallService.getQuoteAsyncService(jsonObject, productCode, authorization);
// }
return response;
}, executor);
listOfCompletableFutures.add(future);
}
for(CompletableFuture<JSONObject> future : listOfCompletableFutures){
responseArray.add(future.get());
}
return new ResponseEntity<Object>(responseArray, responseHeaders, HttpStatus.OK);
}