Java Code Examples for com.google.common.util.concurrent.Futures#catching()

The following examples show how to use com.google.common.util.concurrent.Futures#catching() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IdentityGroup.java    From connector-sdk with Apache License 2.0 6 votes vote down vote up
private static <T> ListenableFuture<T> wrapAsCatchingFuture(
    ListenableFuture<T> result, Executor executor, T defaultValue, String errorMessage) {
  return Futures.catching(
      result,
      IOException.class,
      new Function<IOException, T>() {
        @Override
        @Nullable
        public T apply(@Nullable IOException input) {
          checkNotNull(input);
          logger.log(Level.WARNING, errorMessage, input);
          return defaultValue;
        }
      },
      executor);
}
 
Example 2
Source File: EtcdLockClient.java    From etcd-java with Apache License 2.0 5 votes vote down vote up
@Override
public final ListenableFuture<LockResponse> async(Executor executor) {
    if (lease == null) {
        if (builder.getLease() != 0L) {
            return super.async(executor);
        } else {
            lease = etcdClient.getSessionLease();
        }
    }
    long plId = lease.getLeaseId();
    if (plId != 0L) {
        builder.setLease(plId);
        return super.async(executor);
    }
    ListenableFuture<Long> fut;
    if (deadline == null) {
        fut = lease;
    } else {
        long remainingNanos = deadline.timeRemaining(NANOSECONDS);
        fut = Futures.catching(Futures.withTimeout(lease,
                remainingNanos, NANOSECONDS, grpcClient.getInternalExecutor()),
                TimeoutException.class, te -> {
                    throw Status.DEADLINE_EXCEEDED.withCause(te)
                        .withDescription(String.format("deadline exceeded after %dns",
                            remainingNanos)).asRuntimeException();
                }, MoreExecutors.directExecutor());
    }
    return Futures.transformAsync(fut, id -> {
        builder.setLease(id);
        return super.async(executor);
    }, executor);
}
 
Example 3
Source File: EtcdLeaseClient.java    From etcd-java with Apache License 2.0 5 votes vote down vote up
@Override
public ListenableFuture<LeaseRevokeResponse> revoke(long leaseId, boolean ensureWithRetries) {
    if (!ensureWithRetries) {
        return revoke(leaseId);
    }
    return Futures.catching(client.call(METHOD_LEASE_REVOKE,
            null, LeaseRevokeRequest.newBuilder().setID(leaseId).build(), null,
            (t,r) -> !isNotFound(t) && !closed, true, null, 0L),
            StatusRuntimeException.class, sre -> {
                if (sre.getStatus().getCode() != Code.NOT_FOUND) {
                    throw sre;
                }
                return LeaseRevokeResponse.getDefaultInstance();
            }, directExecutor());
}
 
Example 4
Source File: BuildRulePipelinesRunner.java    From buck with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("CheckReturnValue")
private BuildRulePipelineStage() {
  Futures.catching(future, Throwable.class, throwable -> error = throwable);
}