本文整理汇总了Java中java.util.concurrent.CompletableFuture.exceptionally方法的典型用法代码示例。如果您正苦于以下问题:Java CompletableFuture.exceptionally方法的具体用法?Java CompletableFuture.exceptionally怎么用?Java CompletableFuture.exceptionally使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.CompletableFuture
的用法示例。
在下文中一共展示了CompletableFuture.exceptionally方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: forwardExceptionShouldPropagate
import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
@Test
public void forwardExceptionShouldPropagate() throws Exception {
final String value = UUID.randomUUID().toString();
SettableFuture<String> lf = SettableFuture.create();
CompletableFuture<String> cf = MoreFutures.toCompletableFuture(lf);
Exception intentionalException = new IntentionalException();
final AtomicReference<Throwable> foundException = new AtomicReference<>();
cf = cf.exceptionally(ex -> {
foundException.set(ex);
return value;
});
lf.setException(intentionalException);
assertThat(cf).isDone();
assertThat(cf).isNotCancelled();
assertThat(cf).isNotCompletedExceptionally();
assertThat(cf).isCompletedWithValue(value);
assertThat(foundException.get()).isSameAs(intentionalException);
}
示例2: testExceptionally_normalCompletion
import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
/**
* exceptionally action is not invoked when source completes
* normally, and source result is propagated
*/
public void testExceptionally_normalCompletion() {
for (boolean createIncomplete : new boolean[] { true, false })
for (Integer v1 : new Integer[] { 1, null })
{
final AtomicInteger a = new AtomicInteger(0);
final CompletableFuture<Integer> f = new CompletableFuture<>();
if (!createIncomplete) assertTrue(f.complete(v1));
final CompletableFuture<Integer> g = f.exceptionally
((Throwable t) -> {
a.getAndIncrement();
threadFail("should not be called");
return null; // unreached
});
if (createIncomplete) assertTrue(f.complete(v1));
checkCompletedNormally(g, v1);
checkCompletedNormally(f, v1);
assertEquals(0, a.get());
}}
示例3: testExceptionally_exceptionalCompletion
import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
/**
* exceptionally action completes with function value on source
* exception
*/
public void testExceptionally_exceptionalCompletion() {
for (boolean createIncomplete : new boolean[] { true, false })
for (Integer v1 : new Integer[] { 1, null })
{
final AtomicInteger a = new AtomicInteger(0);
final CFException ex = new CFException();
final CompletableFuture<Integer> f = new CompletableFuture<>();
if (!createIncomplete) f.completeExceptionally(ex);
final CompletableFuture<Integer> g = f.exceptionally
((Throwable t) -> {
ExecutionMode.SYNC.checkExecutionMode();
threadAssertSame(t, ex);
a.getAndIncrement();
return v1;
});
if (createIncomplete) f.completeExceptionally(ex);
checkCompletedNormally(g, v1);
assertEquals(1, a.get());
}}
示例4: testExceptionally_exceptionalCompletionActionFailed
import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
/**
* If an "exceptionally action" throws an exception, it completes
* exceptionally with that exception
*/
public void testExceptionally_exceptionalCompletionActionFailed() {
for (boolean createIncomplete : new boolean[] { true, false })
{
final AtomicInteger a = new AtomicInteger(0);
final CFException ex1 = new CFException();
final CFException ex2 = new CFException();
final CompletableFuture<Integer> f = new CompletableFuture<>();
if (!createIncomplete) f.completeExceptionally(ex1);
final CompletableFuture<Integer> g = f.exceptionally
((Throwable t) -> {
ExecutionMode.SYNC.checkExecutionMode();
threadAssertSame(t, ex1);
a.getAndIncrement();
throw ex2;
});
if (createIncomplete) f.completeExceptionally(ex1);
checkCompletedWithWrappedException(g, ex2);
checkCompletedExceptionally(f, ex1);
assertEquals(1, a.get());
}}
示例5: exchange
import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
/**
* Start a key exchange process
*
* @param groupId
* the id of the group for which a key exchange will be initiated
* @param aHandler
* the handler which succeeds or fails in accordance to the key
* exchange outcome
* @return A Future representation of the key
*/
public CompletableFuture<BigInteger> exchange(int groupId, Handler<AsyncResult<BigInteger>> aHandler) {
conf.getLogger().info(getNode().toString() + Constants.NEGO_CALL + groupId);
Group g = groupMappings.get(groupId);
ExchangeState state = stateMappings.get(groupId);
state.registerHandler(aHandler);
CompletableFuture<Void> res = broadcast(g);
CompletableFuture<BigInteger> future = res.thenCompose(s -> compute(g));
long timer[] = new long[1];
timer[0] = vertx.setTimer(conf.getExchangeTimeout(), id -> {
if (future.isDone() && !future.isCompletedExceptionally()) {
aHandler.handle(Future.succeededFuture());
vertx.cancelTimer(timer[0]);
} else {
aHandler.handle(Future.failedFuture(Constants.EXCEPTIONTIMEOUTEXCEEDED + Constants.NEGO_TIMEOUT));
future.completeExceptionally(
new TimeoutException(Constants.EXCEPTIONTIMEOUTEXCEEDED + conf.getExchangeTimeout()));
vertx.cancelTimer(timer[0]);
}
});
future.exceptionally(e -> {
aHandler.handle(Future.failedFuture(e.getMessage()));
vertx.cancelTimer(timer[0]);
return future.join();
});
return future;
}
示例6: createAndStartContainer
import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
private String createAndStartContainer(CreateContainerCmd cmd, PullStrategy pullStrategy, DockerClient client) {
CompletableFuture<String> respFut = new CompletableFuture<>();
ListImagesCmd imagesCmd = client.listImagesCmd().withImageNameFilter(cmd.getImage());
List<Image> imagesList = imagesCmd.exec();
boolean imageAbsent = imagesList == null || imagesList.size() == 0;
CompletableFuture<Void> pullFut;
if(imageAbsent || pullStrategy.equals(PullStrategy.ALWAYS)) {
pullFut = pullImage(cmd, client);
} else {
pullFut = CompletableFuture.completedFuture(null);
}
pullFut
.exceptionally(ex -> {
logger.warning("An error occurred while executing a docker pull operation: " + ex.getMessage());
return null;
}).thenRun(() -> {
String containerId = startContainer(cmd, client);
respFut.complete(containerId);
})
.exceptionally(ex -> {
respFut.completeExceptionally(ex.getCause());
return null;
});
respFut.exceptionally(ex -> {
logger.severe("Cannot create container. Reason: " + ex.getMessage());
return null;
});
return respFut.join();
}
示例7: performQuerying
import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
private <T> T performQuerying(String serviceName, int expectedRecords, int timeoutInSeconds, int frequencyInSeconds,
BiConsumer<CompletableFuture<T>, Throwable> errorConsumer,
BiConsumer<CompletableFuture<T>, List<SrvRecord>> matchingConsumer) {
CompletableFuture<T> result = new CompletableFuture<>();
final AtomicInteger counter = new AtomicInteger(0);
vertx.setPeriodic(frequencyInSeconds * 1000, timerId -> {
dnsClient.resolveSRV(serviceName, ar -> {
int counterValue = counter.incrementAndGet();
if (ar.succeeded()) {
if (ar.result().size() == expectedRecords) {
vertx.cancelTimer(timerId);
matchingConsumer.accept(result, ar.result());
} else {
if (timedout(timeoutInSeconds, frequencyInSeconds, counterValue)) {
vertx.cancelTimer(timerId);
result.completeExceptionally(new RuntimeException("Discovery timed out."));
}
}
} else {
if (timedout(timeoutInSeconds, frequencyInSeconds, counterValue)) {
vertx.cancelTimer(timerId);
result.completeExceptionally(new RuntimeException("Discovery timed out", ar.cause()));
} else {
errorConsumer.accept(result, ar.cause());
}
}
});
});
result.exceptionally(ex -> {
throw new RuntimeException("Discovery/cleanup failed for service " + serviceName);
});
return result.join();
}
示例8: testCompletionStage_exceptionally_wraps
import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
@Test public void testCompletionStage_exceptionally_wraps() throws Exception {
CompletableFuture<String> future = new CompletableFuture<>();
MyException exception = new MyException("test");
future.completeExceptionally(exception);
CompletionStage<String> stage = future.exceptionally(e -> {
throw new CompletionException(e);
});
assertCauseOf(ExecutionException.class, stage).isSameAs(exception);
}