我有一个服务,使2调用另一个服务,然后组合的响应,并生成一个响应。
public HashMap<String,Double> getResp(String requestJson, double defaultScore, String anomalySelfInclUrl,String anomalySelfExclUrl){
CompletableFuture<Double> f1 = getHelper(requestJson,anomalySelfInclUrl);
CompletableFuture <Double>f2= getHelper(requestJson,anomalySelfExclUrl);
AnomalyResponse anomalyResponse =new AnomalyResponse();
HashMap<String,Double> respMap= new HashMap<>();
CompletableFuture.allOf(f1,f2)
.thenRun(() -> {
try {
Double anomalySelfModelRes = f1.get();
Double anomalyExclModelRes = f2.get();
respMap.put(Constants.selfInc,anomalySelfModelRes);
respMap.put(Constants.selfExcl,anomalyExclModelRes);
} catch (Exception e) {
e.printStackTrace();
}
}).join();
return respMap;
}
public CompletableFuture<Double> getHelper(String requestJson, String url) {
return CompletableFuture.supplyAsync(() -> {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>(requestJson, headers);
String resp =restTemplate.postForObject(url, entity, String.class);
JsonObject json = new JsonParser().parse(resp).getAsJsonObject();
return json.get("score").getAsDouble();
});
}
在getResp方法中,我使用可完成的未来来创建2个帖子调用(通过getHelper方法)。一旦两个帖子调用完成,我将它们添加到HashMap并返回。但是我在postForObject()方法中得到以下错误。
org.jboss.resteasy.spi.UnhandledException: java.util.concurrent.CompletionException
错误似乎发生了,因为被调用的服务没有收到发布的json数据和标头(包装在HttpEntity中)。这怎么会发生呢?此问题不会在顺序调用期间出现。
如果在completable future内执行该方法时出现异常,它将抛出completion exception。
假设您从API中获得401
您可以从objOfCompletionException获得实际响应。getCause()
方法调用。它将包含实际发生异常的所有细节。
我有一个相当普遍或独特的要求。例如,我有以下列表:
我将以下响应返回给用户 到目前为止,我正在进行三次连续调用来计算这个,每个调用都可以独立于其他调用运行。我尝试制作三种不同的作为: 如果我做了
问题内容: 我正在尝试创建86个task.py实例以同时运行。 问题答案: 等待命令完成。改为使用:
0信息如果它以ok结尾就可以工作1详细cli['C:\程序文件\nodejs\node.exe',1详细cli'C:\users\bhargavii nadendla\appdata\roaming\npm\node_module\npm\bin\npm-cli.js',1详细cli'run',1详细cli'build']2使用npm@6.0.1 3使用node@v8.11.2 4详细run-sc
我必须运行多个外部调用操作,然后以列表的形式获得结果。我决定使用api,而我准备的代码相当恶心: 示例: 我有以下几个问题: > 我可以避免在流中重复块吗?在流中我将CompletableFuture映射到User? 此代码是否可以不那么连续(如何避免等待所有的未来完成?) 这样做可以吗(所有的未来都将在流中解决吗?):