:
$('#loaderImage').show(); $http.get('/utilities/longProcess') .success(function(data, status, headers, config) { console.log('Completed'); $scope.sampleJSON = data.pmdStructureWrapper; $scope.sampleJSONDuplicates = data.pmdDuplicates; $scope.$watch('sampleJSON', setTimeout(function() { $('.panel-body li').each(function() { if ($.trim($(this).text()) === "") { $(this).hide(); } }); }, 1000)); $('#loaderImage').hide(); }) .error(function(data, status, header, config) { });
控制器:
@RequestMapping("/utilities/longProcess") public DeferredResult<String> async(HttpServletResponse response, HttpServletRequest request) { DeferredResult<String> dr = new DeferredResult<>(); CompletableFuture.supplyAsync(() -> { return callURL(response, request); }, ex).thenAccept((String message) -> { dr.setResult(message); }); return dr; } private String callURL(HttpServletResponse response, HttpServletRequest request){ PMDMainWrapper pmdMainWrapper = new PMDMainWrapper(); Map<String, PMDStructureWrapper> codeReviewByClass = new HashMap<>(); String partnerURL = this.partnerURL; String toolingURL = this.toolingURL; Cookie[] cookies = request.getCookies(); List<PMDStructure> violationStructure = null; try { violationStructure = metadataLoginUtil.startReviewer(partnerURL, toolingURL, cookies); } catch (Exception e) { e.printStackTrace(); } PMDStructureWrapper pmdStructureWrapper = null; List<PMDStructure> pmdStructureList = null; List<PMDStructure> pmdDuplicatesList = new ArrayList<>(); int size = violationStructure.size(); long start = System.currentTimeMillis(); for (int i = 0; i < size; i++) { if (codeReviewByClass.containsKey(violationStructure.get(i).getName())) { PMDStructureWrapper pmdStructureWrapper1 = codeReviewByClass.get(violationStructure.get(i).getName()); List<PMDStructure> pmdStructures = pmdStructureWrapper1.getPmdStructures(); pmdStructures.add(violationStructure.get(i)); pmdStructureWrapper1.setPmdStructures(pmdStructures); } else { pmdStructureList = new ArrayList<>(); pmdStructureList.add(violationStructure.get(i)); pmdStructureWrapper = new PMDStructureWrapper(); pmdStructureWrapper.setPmdStructures(pmdStructureList); codeReviewByClass.put(violationStructure.get(i).getName(), pmdStructureWrapper); } } long stop = System.currentTimeMillis(); LOGGER.info("Total Time Taken from PMDController "+ String.valueOf(stop-start)); if (!codeReviewByClass.isEmpty()) { pmdMainWrapper.setPmdStructureWrapper(codeReviewByClass); pmdMainWrapper.setPmdDuplicates(pmdDuplicatesList); Gson gson = new GsonBuilder().create(); return gson.toJson(pmdMainWrapper); } return ""; }
我正在使用异步流程,因为当应用程序托管在heroku中时,将结果返回页面需要大约120秒的时间,但是根据heroku文档,其余api应该在30秒内返回,否则它将终止该过程,
但是仍然在实现上述逻辑之后,仍然看到超时错误。我在javascript中保留了一个控制台日志,console.log('Completed');但是仅当它从callURL方法返回结果(超过120秒才能返回)时才打印该日志。
console.log('Completed');
callURL
我要实现的是,当UI发送请求时,它应该继续接收一条消息,指出该消息仍在加载中,以便请求不会超时?
CompletableFuture.supplyAsync()在不同的线程中运行指定的供应商(ForkJoinThreadPool默认为)。thenAccept()方法仅在上一次执行返回后运行。因此,在您的情况下它不会很快返回,您只是在另一个线程中调用长时间运行的调用。
CompletableFuture.supplyAsync()
ForkJoinThreadPool
thenAccept()
而是定义一个公共对象,该对象充当缓存(例如HttpSession),并CompletableFuture返回存储在其中的对象。并callURL()仅在缓存为空时执行:
HttpSession
CompletableFuture
callURL()
@RequestMapping("/utilities/longProcess") public CompletableFuture<String> async(HttpServletResponse response, HttpServletRequest request) { HttpSession session = request.getSession(); return CompletableFuture.supplyAsync(() -> session.getAttribute("CACHED_RESULT")) .thenComposeAsync(obj -> { if (obj == null) { CompletableFuture.supplyAsync(() -> callUrl(request, response)) .thenAccept(result -> session.setAttribute("CACHED_RESULT", result)); return CompletableFuture.completedFuture("not ready yet"); } return CompletableFuture.completedFuture(obj.toString()); });
您还可以添加一个时间戳记,以查看上次拨打电话的时间,callUrl()以及拨打callUrl()电话但尚未收到答案时不再打给您的电话。
callUrl()