[英]How do you complete a CompletableFuture when another set of CompletableFutures is complete?
[英]How to set complete or failure to CompletableFuture?
如果我在该方法中遇到异常,该如何在stopTask()方法中设置CompletableFuture完成或失败操作? 我有一些代码:
private static class SenderTask implements Runnable {
private final Sender sender;
private ScheduledFuture<?> task;
@Override
public void run() {
try {
LOGGER.debug("SenderTask {} calling.", sender.getName());
sender.process();
} catch (Exception e) {
LOGGER.error(e);
}
}
public CompletableFuture<SenderTask> stopTask() {
return CompletableFuture.supplyAsync(() -> {
task.cancel(false);
LOGGER.info("Sender {} try stopping.", sender.getName());
try {
task.get(sender.getSenderConfig().getTimeoutConfig().getSendFileTimeout(),` TimeUnit.MILLISECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
LOGGER.error(e);
}
return this;
});
}
}
我需要知道任务何时停止。
可以使用这样的句柄:
public CompletableFuture<SenderTask> stopTask() {
return CompletableFuture.supplyAsync(() -> {
task.cancel(false);
LOGGER.info("Sender {} try stopping.", sender.getName()); task.get(sender.getSenderConfig().getTimeoutConfig().getSendFileTimeout(),TimeUnit.MILLISECONDS);
return this;
}).handle((res, ex) -> {
if(ex != null) {
LOGGER.error(ex);
...
}
return res;
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.