本文整理汇总了Java中java.util.concurrent.CompletableFuture.completeExceptionally方法的典型用法代码示例。如果您正苦于以下问题:Java CompletableFuture.completeExceptionally方法的具体用法?Java CompletableFuture.completeExceptionally怎么用?Java CompletableFuture.completeExceptionally使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.CompletableFuture
的用法示例。
在下文中一共展示了CompletableFuture.completeExceptionally方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: closeConnectionAsync
import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
@Override
public CompletionStage<ClusterConnectionReport> closeConnectionAsync(
SocketAddress connection, CloseType type) {
for (BoundNode node : this.getNodes()) {
// identify the node that has the connection and close it with that node.
for (SocketAddress address : node.getConnections().getConnections()) {
if (connection.equals(address)) {
return node.closeConnectionAsync(address, type)
.thenApply(NodeConnectionReport::getRootReport);
}
}
}
CompletableFuture<ClusterConnectionReport> failedFuture = new CompletableFuture<>();
failedFuture.completeExceptionally(new IllegalArgumentException("Not found"));
return failedFuture;
}
示例2: run
import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
private static <T> CompletableFuture<T> run(ThriftCall<T> call) {
ThriftCompletableFuture<T> future = new ThriftCompletableFuture<>();
try {
call.apply(future);
} catch (Exception e) {
final Throwable cause;
if (e instanceof InvocationTargetException) {
cause = MoreObjects.firstNonNull(e.getCause(), e);
} else {
cause = e;
}
CompletableFuture<T> failedFuture = new CompletableFuture<>();
failedFuture.completeExceptionally(cause);
return failedFuture;
}
return future;
}
示例3: onConnectResponse
import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
private void onConnectResponse(final String shardName, final long cookie,
final CompletableFuture<ShardBackendInfo> future, final Object response, final Throwable failure) {
if (failure != null) {
LOG.debug("Connect attempt to {} failed, will retry", shardName, failure);
future.completeExceptionally(wrap("Connection attempt failed", failure));
return;
}
if (response instanceof RequestFailure) {
final Throwable cause = ((RequestFailure<?, ?>) response).getCause().unwrap();
LOG.debug("Connect attempt to {} failed to process", shardName, cause);
final Throwable result = cause instanceof NotLeaderException
? wrap("Leader moved during establishment", cause) : cause;
future.completeExceptionally(result);
return;
}
LOG.debug("Resolved backend information to {}", response);
Preconditions.checkArgument(response instanceof ConnectClientSuccess, "Unhandled response %s",
response);
final ConnectClientSuccess success = (ConnectClientSuccess) response;
future.complete(new ShardBackendInfo(success.getBackend(), nextSessionId.getAndIncrement(),
success.getVersion(), shardName, UnsignedLong.fromLongBits(cookie), success.getDataTree(),
success.getMaxMessages()));
}
示例4: storeMeter
import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
@Override
public CompletableFuture<MeterStoreResult> storeMeter(Meter meter) {
CompletableFuture<MeterStoreResult> future = new CompletableFuture<>();
MeterKey key = MeterKey.key(meter.deviceId(), meter.id());
futures.put(key, future);
MeterData data = new MeterData(meter, null, local);
try {
meters.put(key, data);
} catch (StorageException e) {
future.completeExceptionally(e);
}
return future;
}
示例5: testThenCompose_exceptionalCompletion
import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
/**
* thenCompose result completes exceptionally after exceptional
* completion of source
*/
public void testThenCompose_exceptionalCompletion() {
for (ExecutionMode m : ExecutionMode.values())
for (boolean createIncomplete : new boolean[] { true, false })
{
final CFException ex = new CFException();
final CompletableFutureInc r = new CompletableFutureInc(m);
final CompletableFuture<Integer> f = new CompletableFuture<>();
if (!createIncomplete) f.completeExceptionally(ex);
final CompletableFuture<Integer> g = m.thenCompose(f, r);
if (createIncomplete) f.completeExceptionally(ex);
checkCompletedWithWrappedException(g, ex);
checkCompletedExceptionally(f, ex);
r.assertNotInvoked();
}}
示例6: testMinimalCompletionStage_toCompletableFuture_exceptionalCompletion
import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
/**
* minimalStage.toCompletableFuture() returns a CompletableFuture that
* is completed exceptionally when source is.
*/
public void testMinimalCompletionStage_toCompletableFuture_exceptionalCompletion() {
for (boolean createIncomplete : new boolean[] { true, false })
{
CFException ex = new CFException();
CompletableFuture<Integer> f = new CompletableFuture<>();
CompletionStage<Integer> minimal = f.minimalCompletionStage();
if (!createIncomplete) f.completeExceptionally(ex);
CompletableFuture<Integer> g = minimal.toCompletableFuture();
if (createIncomplete) {
checkIncomplete(f);
checkIncomplete(g);
f.completeExceptionally(ex);
}
checkCompletedExceptionally(f, ex);
checkCompletedWithWrappedException(g, ex);
}}
示例7: testCompleteExceptionally
import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
/**
* completeExceptionally completes exceptionally, as indicated by
* methods isDone, isCancelled, join, get, and getNow
*/
public void testCompleteExceptionally() {
CompletableFuture<Integer> f = new CompletableFuture<>();
CFException ex = new CFException();
checkIncomplete(f);
f.completeExceptionally(ex);
checkCompletedExceptionally(f, ex);
}
示例8: invokeFunction
import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
@Override
public CompletableFuture<com.fnproject.fn.api.flow.HttpResponse> invokeFunction(String fnId, HttpMethod method, Headers headers, byte[] data) {
FnFunctionStub stub = functionStubs
.computeIfAbsent(fnId, (k) -> {
throw new IllegalStateException("Function was invoked that had no definition: " + k);
});
try {
return CompletableFuture.completedFuture(stub.stubFunction(method, headers, data));
} catch (Exception e) {
CompletableFuture<com.fnproject.fn.api.flow.HttpResponse> respFuture = new CompletableFuture<>();
respFuture.completeExceptionally(e);
return respFuture;
}
}
示例9: whenReceivingResponse
import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
@Override
CompletableFuture<Void> whenReceivingResponse() {
CompletableFuture<Void> cf = new MinimalFuture<>();
try {
ReceiveResponseEvent evt = new ReceiveResponseEvent(cf);
client.registerEvent(evt);
} catch (IOException e) {
cf.completeExceptionally(e);
}
return cf;
}
示例10: resolveProductTypeReference_WithExceptionOnProductTypeFetch_ShouldNotResolveReference
import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
@Test
public void resolveProductTypeReference_WithExceptionOnProductTypeFetch_ShouldNotResolveReference() {
final ProductDraftBuilder productBuilder = getBuilderWithProductTypeRefId(PRODUCT_TYPE_ID)
.key("dummyKey");
final CompletableFuture<Optional<String>> futureThrowingSphereException = new CompletableFuture<>();
futureThrowingSphereException.completeExceptionally(new SphereException("CTP error on fetch"));
when(productTypeService.fetchCachedProductTypeId(anyString())).thenReturn(futureThrowingSphereException);
assertThat(referenceResolver.resolveProductTypeReference(productBuilder).toCompletableFuture())
.hasFailed()
.hasFailedWithThrowableThat()
.isExactlyInstanceOf(SphereException.class)
.hasMessageContaining("CTP error on fetch");
}
示例11: create
import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
@Override
public CompletableFuture<U> create(final C credentials, final AsyncWebContext context) {
try {
final CompletableFuture<T> tokenFuture = new CompletableFuture<>();
try {
tokenFuture.complete(getAccessToken(credentials));
} catch (HttpAction action) {
tokenFuture.completeExceptionally(action);
}
return tokenFuture.thenCompose(this::retrieveUserProfileFromToken);
} catch (final OAuthException e) {
throw new TechnicalException(e);
}
}
示例12: testNonTransactionalWriterProcessElementAccounting
import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
/**
* Tests the accounting of pending writes.
*/
@Test
public void testNonTransactionalWriterProcessElementAccounting() throws Exception {
try (NonTransactionalWriterTestContext context = new NonTransactionalWriterTestContext(PravegaWriterMode.ATLEAST_ONCE)) {
try (StreamSinkOperatorTestHarness<Integer> testHarness = createTestHarness(context.sinkFunction)) {
testHarness.open();
FlinkPravegaWriter.NonTransactionalWriter internalWriter = (FlinkPravegaWriter.NonTransactionalWriter) context.sinkFunction.writer;
CompletableFuture<Void> e1Future = context.prepareWrite();
StreamRecord<Integer> e1 = new StreamRecord<>(1, 1L);
testHarness.processElement(e1);
Assert.assertEquals(1, internalWriter.pendingWritesCount.get());
CompletableFuture<Void> e2Future = context.prepareWrite();
StreamRecord<Integer> e2 = new StreamRecord<>(2, 2L);
testHarness.processElement(e2);
Assert.assertEquals(2, internalWriter.pendingWritesCount.get());
CompletableFuture<Void> e3Future = context.prepareWrite();
StreamRecord<Integer> e3 = new StreamRecord<>(3, 3L);
testHarness.processElement(e3);
Assert.assertEquals(3, internalWriter.pendingWritesCount.get());
e1Future.complete(null);
e2Future.completeExceptionally(new IntentionalRuntimeException());
e3Future.complete(null);
Assert.assertEquals(0, internalWriter.pendingWritesCount.get());
// clear the error for test simplicity
internalWriter.writeError.set(null);
}
}
}
示例13: testRunAfterBoth_exceptionalCompletion
import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
/**
* runAfterBoth result completes exceptionally after exceptional
* completion of either source
*/
public void testRunAfterBoth_exceptionalCompletion() throws Throwable {
for (ExecutionMode m : ExecutionMode.values())
for (boolean fFirst : new boolean[] { true, false })
for (boolean failFirst : new boolean[] { true, false })
for (Integer v1 : new Integer[] { 1, null })
{
final CompletableFuture<Integer> f = new CompletableFuture<>();
final CompletableFuture<Integer> g = new CompletableFuture<>();
final CFException ex = new CFException();
final Noop r1 = new Noop(m);
final Noop r2 = new Noop(m);
final Noop r3 = new Noop(m);
final CompletableFuture<Integer> fst = fFirst ? f : g;
final CompletableFuture<Integer> snd = !fFirst ? f : g;
final Callable<Boolean> complete1 = failFirst ?
() -> fst.completeExceptionally(ex) :
() -> fst.complete(v1);
final Callable<Boolean> complete2 = failFirst ?
() -> snd.complete(v1) :
() -> snd.completeExceptionally(ex);
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
assertTrue(complete1.call());
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
checkIncomplete(h1);
checkIncomplete(h2);
assertTrue(complete2.call());
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
checkCompletedWithWrappedException(h1, ex);
checkCompletedWithWrappedException(h2, ex);
checkCompletedWithWrappedException(h3, ex);
r1.assertNotInvoked();
r2.assertNotInvoked();
r3.assertNotInvoked();
checkCompletedNormally(failFirst ? snd : fst, v1);
checkCompletedExceptionally(failFirst ? fst : snd, ex);
}}
示例14: wrapFuture_futureBecomesExpectedFailure
import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
@Test public void wrapFuture_futureBecomesExpectedFailure() throws Exception {
CompletableFuture<String> future = new CompletableFuture<>();
CompletionStage<Maybe<String, MyException>> stage =
Maybe.catchException(MyException.class, future);
assertPending(stage);
MyException exception = new MyException("test");
future.completeExceptionally(exception);
assertCompleted(stage).isEqualTo(Maybe.except(exception));
}
示例15: connectAsync
import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
@Override
public CompletableFuture<Void> connectAsync() {
CompletableFuture<Void> plainFuture = new MinimalFuture<>();
try {
chan.configureBlocking(false);
chan.connect(address);
client.registerEvent(new ConnectEvent(plainFuture));
} catch (IOException e) {
plainFuture.completeExceptionally(e);
}
return plainFuture;
}