Js:
$('#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秒才能返回。
我想要实现的是当UI发送请求时,它应该继续收到一条仍然加载的消息,以便请求不会超时?
答案 0 :(得分:1)
CompletableFuture.supplyAsync()
在另一个线程中运行指定的供应商(默认情况下,来自ForkJoinThreadPool
的一个)。 thenAccept()
方法仅在上一次执行返回后运行。因此,在你的情况下它不会快速返回,你只是在另一个线程中调用长时间运行的调用。
相反,定义一个公共对象,它充当缓存(例如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()
爱好。