gpt4 book ai didi

java - Spring 启动: Async request not returning anything

转载 作者:行者123 更新时间:2023-11-30 06:13:37 25 4
gpt4 key购买 nike

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) {

});

Controller :

@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 发送请求时,它应该不断接收一条表示仍在加载的消息,以便请求不会超时?

最佳答案

CompletableFuture.supplyAsync() 在不同的线程中运行指定的供应商(默认情况下来自 ForkJoinThreadPool)。 thenAccept() 方法仅在上一次执行返回后运行。因此,在您的情况下,它不会快速返回,您只是在不同的线程中调用长时间运行的调用。

相反,定义一个公共(public)对象,充当缓存(例如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()调用但尚未收到答复。

关于java - Spring 启动: Async request not returning anything,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49726559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com