Example usage for com.google.common.util.concurrent SettableFuture setException

List of usage examples for com.google.common.util.concurrent SettableFuture setException

Introduction

In this page you can find the example usage for com.google.common.util.concurrent SettableFuture setException.

Prototype

@Override
    public boolean setException(Throwable throwable) 

Source Link

Usage

From source file:com.google.gapid.server.GapitPkgInfoProcess.java

@Override
protected OutputHandler<PkgInfo.PackageList> createStdoutHandler() {
    return new BinaryHandler<PkgInfo.PackageList>(in -> {
        byte[] data = ByteStreams.toByteArray(in);
        return (data.length == 0) ? null : PkgInfo.PackageList.parseFrom(data);
    }) {/*from   ww w .  ja v  a  2s  .  co  m*/
        @Override
        public void finish(SettableFuture<PackageList> result) throws InterruptedException {
            if (!result.isDone()) {
                result.setException(new Exception("The gapit command didn't produce any output!"));
            }
        }
    };
}

From source file:org.retrostore.android.view.ImageLoader.java

public ListenableFuture<Bitmap> loadAsBitmapAsync(final String url, final int width, final int height) {
    final SettableFuture<Bitmap> future = SettableFuture.create();
    final Context context = mCtx.get();
    if (context == null) {
        future.setException(new RuntimeException("Context invalid."));
        return future;
    }/*from w w w .ja  v a 2 s .co m*/

    mExecutor.execute(new Runnable() {
        @Override
        public void run() {
            try {
                future.set(Glide.with(context).load(url).asBitmap().into(width, height).get());
            } catch (InterruptedException | ExecutionException e) {
                Log.e(TAG, "Could not load image as bitmap.", e);
                future.setException(e);
            }
        }
    });
    return future;
}

From source file:org.apache.helix.provisioning.yarn.NMCallbackHandler.java

@Override
public void onStopContainerError(ContainerId containerId, Throwable t) {
    LOG.error("Failed to stop Container " + containerId, t);
    SettableFuture<ContainerStopResponse> settableFuture = applicationMaster.containerStopMap
            .remove(containerId);/*  w w  w.  jav  a  2s.co  m*/
    settableFuture.setException(t);
    containers.remove(containerId);
}

From source file:com.andrewkroh.cisco.xmlservices.XmlResponseChannelHandler.java

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    if (LOGGER.isWarnEnabled()) {
        IpPhone phone = ctx.channel().attr(phoneAttributeKey).get();
        LOGGER.warn("Exception in handler for {}.", phone.getHostname());
    }/* www. j  a va2s. c om*/

    SettableFuture<XmlPushResponse> responseFuture = ctx.channel().attr(responseAttributeKey).get();
    responseFuture.setException(cause);

    // Cleanup:
    ctx.close();
    ctx.channel().close();
}

From source file:org.apache.omid.tso.client.MockTSOClient.java

@Override
public TSOFuture<Long> commit(long transactionId, Set<? extends CellId> cells) {
    synchronized (conflictMap) {
        SettableFuture<Long> f = SettableFuture.create();
        if (transactionId < lwm.get()) {
            f.setException(new AbortException());
            return new ForwardingTSOFuture<>(f);
        }//from  ww w  . j a  v a2 s  .  c o m

        boolean canCommit = true;
        for (CellId c : cells) {
            int index = Math.abs((int) (c.getCellId() % CONFLICT_MAP_SIZE));
            if (conflictMap[index] >= transactionId) {
                canCommit = false;
                break;
            }
        }

        if (canCommit) {
            long commitTimestamp = timestampGenerator.incrementAndGet();
            for (CellId c : cells) {
                int index = Math.abs((int) (c.getCellId() % CONFLICT_MAP_SIZE));
                long oldVal = conflictMap[index];
                conflictMap[index] = commitTimestamp;
                long curLwm = lwm.get();
                while (oldVal > curLwm) {
                    if (lwm.compareAndSet(curLwm, oldVal)) {
                        break;
                    }
                    curLwm = lwm.get();
                }
            }

            f.set(commitTimestamp);
            try {
                commitTable.addCommittedTransaction(transactionId, commitTimestamp);
                commitTable.updateLowWatermark(lwm.get());
                commitTable.flush();
            } catch (IOException ioe) {
                f.setException(ioe);
            }
        } else {
            f.setException(new AbortException());
        }
        return new ForwardingTSOFuture<>(f);
    }
}

From source file:org.apache.helix.provisioning.yarn.NMCallbackHandler.java

@Override
public void onStartContainerError(ContainerId containerId, Throwable t) {
    LOG.error("Failed to start Container " + containerId, t);
    SettableFuture<ContainerLaunchResponse> settableFuture = applicationMaster.containerLaunchResponseMap
            .remove(containerId);//from w w w. java2 s . c  o m
    settableFuture.setException(t);
    containers.remove(containerId);
}

From source file:com.google.gapid.server.GapitPkgInfoProcess.java

@Override
protected OutputHandler<PackageList> createStderrHandler() {
    StringBuilder output = new StringBuilder();
    return new LoggingStringHandler<PackageList>(LOG, name, true, (line) -> {
        output.append(line).append('\n');
        return null;
    }) {/*from  w  w w .j  av  a  2 s .c  o  m*/
        @Override
        public void finish(SettableFuture<PackageList> result) throws InterruptedException {
            if (!result.isDone() && output.length() > 0) {
                result.setException(new Exception("The gapit command failed:\n" + output.toString()));
            }
        }
    };
}

From source file:com.yahoo.omid.tsoclient.MockTSOClient.java

public TSOFuture<Long> commit(long transactionId, Set<? extends CellId> cells) {
    synchronized (conflictMap) {
        SettableFuture<Long> f = SettableFuture.<Long>create();
        if (transactionId < lwm.get()) {
            f.setException(new AbortException());
            return new ForwardingTSOFuture<Long>(f);
        }/*from   w  ww  .  java  2s  .  c  o m*/

        boolean canCommit = true;
        for (CellId c : cells) {
            int index = Math.abs((int) (c.getCellId() % (long) CONFLICT_MAP_SIZE));
            if (conflictMap[index] >= transactionId) {
                canCommit = false;
                break;
            }
        }

        if (canCommit) {
            long commitTimestamp = timestampGenerator.incrementAndGet();
            for (CellId c : cells) {
                int index = Math.abs((int) (c.getCellId() % (long) CONFLICT_MAP_SIZE));
                long oldVal = conflictMap[index];
                conflictMap[index] = commitTimestamp;
                long curLwm = lwm.get();
                while (oldVal > curLwm) {
                    if (lwm.compareAndSet(curLwm, oldVal)) {
                        break;
                    }
                    curLwm = lwm.get();
                }
            }

            f.set(commitTimestamp);
            try {
                commitTable.addCommittedTransaction(transactionId, commitTimestamp);
                commitTable.updateLowWatermark(lwm.get());
                commitTable.flush().get();
            } catch (IOException ioe) {
                f.setException(ioe);
            } catch (InterruptedException ie) {
                Thread.currentThread().interrupt();
                f.setException(ie);
            } catch (ExecutionException ee) {
                f.setException(ee.getCause());
            }
        } else {
            f.setException(new AbortException());
        }
        return new ForwardingTSOFuture<Long>(f);
    }
}

From source file:org.apache.beam.runners.fnexecution.control.FnApiControlClient.java

/** Closes this client and terminates any outstanding requests exceptionally. */
private synchronized void closeAndTerminateOutstandingRequests(Throwable cause) {
    if (isClosed) {
        return;// www  . j  a v  a2  s.  c  om
    }

    // Make a copy of the map to make the view of the outstanding requests consistent.
    Map<String, SettableFuture<BeamFnApi.InstructionResponse>> outstandingRequestsCopy = new ConcurrentHashMap<>(
            outstandingRequests);
    outstandingRequests.clear();
    isClosed = true;

    if (outstandingRequestsCopy.isEmpty()) {
        requestReceiver.onCompleted();
        return;
    }
    requestReceiver.onError(new StatusRuntimeException(Status.CANCELLED.withDescription(cause.getMessage())));

    LOG.error("{} closed, clearing outstanding requests {}", FnApiControlClient.class.getSimpleName(),
            outstandingRequestsCopy);
    for (SettableFuture<BeamFnApi.InstructionResponse> outstandingRequest : outstandingRequestsCopy.values()) {
        outstandingRequest.setException(cause);
    }
}

From source file:org.openqa.selenium.safari.WebSocketConnection.java

private void handleUncaughtException(Throwable t) {
    SettableFuture<String> response = pendingResponse.getAndSet(null);
    if (null != response) {
        response.setException(t);
    }//ww w . j  a  v a2 s .  c o  m
}