當前位置: 首頁>>代碼示例>>Java>>正文


Java CompletableFuture.completeExceptionally方法代碼示例

本文整理匯總了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;
}
 
開發者ID:datastax,項目名稱:simulacron,代碼行數:19,代碼來源:BoundCluster.java

示例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;
}
 
開發者ID:line,項目名稱:centraldogma,代碼行數:18,代碼來源:DefaultCentralDogma.java

示例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()));
}
 
開發者ID:hashsdn,項目名稱:hashsdn-controller,代碼行數:25,代碼來源:AbstractShardBackendResolver.java

示例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;

}
 
開發者ID:shlee89,項目名稱:athena,代碼行數:17,代碼來源:DistributedMeterStore.java

示例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();
}}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:20,代碼來源:CompletableFutureTest.java

示例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);
}}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:21,代碼來源:CompletableFutureTest.java

示例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);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:12,代碼來源:CompletableFutureTest.java

示例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;
    }
}
 
開發者ID:fnproject,項目名稱:fdk-java,代碼行數:16,代碼來源:FnTestingRule.java

示例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;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:12,代碼來源:PlainHttpConnection.java

示例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");
}
 
開發者ID:commercetools,項目名稱:commercetools-sync-java,代碼行數:16,代碼來源:ProductTypeReferenceResolverTest.java

示例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);
    }
}
 
開發者ID:millross,項目名稱:pac4j-async,代碼行數:15,代碼來源:AsyncOAuthProfileCreator.java

示例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);
        }
    }
}
 
開發者ID:pravega,項目名稱:flink-connectors,代碼行數:36,代碼來源:FlinkPravegaWriterTest.java

示例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);
}}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:44,代碼來源:CompletableFutureTest.java

示例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));
}
 
開發者ID:google,項目名稱:mug,代碼行數:10,代碼來源:MaybeTest.java

示例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;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:13,代碼來源:PlainHttpConnection.java


注:本文中的java.util.concurrent.CompletableFuture.completeExceptionally方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。