Example usage for com.google.common.util.concurrent Futures immediateFailedFuture

List of usage examples for com.google.common.util.concurrent Futures immediateFailedFuture

Introduction

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

Prototype

@CheckReturnValue
public static <V> ListenableFuture<V> immediateFailedFuture(Throwable throwable) 

Source Link

Document

Returns a ListenableFuture which has an exception set immediately upon construction.

Usage

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

public ListenableFuture<T> start() {
    final ProcessBuilder pb = new ProcessBuilder();
    // Use the base directory as the working directory for the server.
    pb.directory(GapiPaths.base());// w  ww.j av a2s . c  om
    Exception prepareError = prepare(pb);
    if (prepareError != null) {
        return Futures.immediateFailedFuture(prepareError);
    }

    final SettableFuture<T> result = SettableFuture.create();
    serverThread = new Thread(ChildProcess.class.getName() + "-" + name) {
        @Override
        public void run() {
            runProcess(result, pb);
        }
    };
    serverThread.start();
    return result;
}

From source file:com.microsoft.tooling.msservices.serviceexplorer.NodeActionListener.java

public ListenableFuture<Void> actionPerformedAsync(NodeActionEvent e) {
    try {/*from ww  w . j ava2 s .com*/
        actionPerformed(e);
        return Futures.immediateFuture(null);
    } catch (AzureCmdException ex) {
        return Futures.immediateFailedFuture(ex);
    }
}

From source file:net.javacrumbs.futureconverter.common.test.guava.GuavaOriginalFutureTestHelper.java

@Override
public ListenableFuture<String> createExceptionalFuture(Exception exception) {
    return Futures.immediateFailedFuture(exception);
}

From source file:com.orangerhymelabs.helenus.cassandra.table.ViewService.java

public ListenableFuture<View> create(View view) {
    ListenableFuture<Boolean> tableFuture = tables.exists(view.databaseName(), view.tableName());
    return Futures.transformAsync(tableFuture, new AsyncFunction<Boolean, View>() {
        @Override/*from w  ww. j a  v a 2 s  .c o m*/
        public ListenableFuture<View> apply(Boolean exists) throws Exception {
            if (exists) {
                try {
                    ValidationEngine.validateAndThrow(view);
                    return views.create(view);
                } catch (ValidationException e) {
                    return Futures.immediateFailedFuture(e);
                }
            } else {
                return Futures.immediateFailedFuture(
                        new ItemNotFoundException("Table not found: " + view.tableName()));
            }
        }
    });
}

From source file:com.facebook.buck.util.network.HttpPutLogger.java

@Override
protected ListenableFuture<Void> logMultiple(ImmutableCollection<BatchEntry> data) {
    StringWriter stringWriter = new StringWriter();
    try {//w w w  .j  a  v  a 2s. c o  m
        JsonGenerator jsonGenerator = objectMapper.getFactory().createGenerator(stringWriter);
        jsonGenerator.writeStartArray();
        for (BatchEntry entry : data) {
            jsonGenerator.writeRawValue(entry.getLine());
        }
        jsonGenerator.writeEndArray();
        jsonGenerator.close();
        return Futures.transform(endpoint.post(stringWriter.toString()), new Function<HttpResponse, Void>() {
            @Nullable
            @Override
            public Void apply(HttpResponse input) {
                return null;
            }
        });
    } catch (IOException e) {
        return Futures.immediateFailedFuture(e);
    }
}

From source file:io.v.v23.syncbase.nosql.RowImpl.java

@Override
public ListenableFuture<Void> put(VContext ctx, Object value, Type type) {
    try {/*from  www.j ava2 s.  c o  m*/
        byte[] data = VomUtil.encode(value, type);
        return client.put(ctx, schemaVersion, data);
    } catch (VException e) {
        return Futures.immediateFailedFuture(e);
    }
}

From source file:zipkin2.collector.scribe.ScribeSpanConsumer.java

@Override
public ListenableFuture<ResultCode> log(List<LogEntry> messages) {
    metrics.incrementMessages();/*from   w  w  w. j av  a2  s  . c o m*/
    List<Span> spans;
    try {
        spans = messages.stream().filter(m -> m.category.equals(category))
                .map(m -> m.message.getBytes(StandardCharsets.ISO_8859_1))
                .map(b -> Base64.getMimeDecoder().decode(b)) // finagle-zipkin uses mime encoding
                .peek(b -> metrics.incrementBytes(b.length)).map(SpanBytesDecoder.THRIFT::decodeOne)
                .collect(Collectors.toList());
    } catch (RuntimeException e) {
        metrics.incrementMessagesDropped();
        return Futures.immediateFailedFuture(e);
    }

    SettableFuture<ResultCode> result = SettableFuture.create();
    collector.accept(spans, new Callback<Void>() {
        @Override
        public void onSuccess(@Nullable Void value) {
            result.set(ResultCode.OK);
        }

        @Override
        public void onError(Throwable t) {
            result.setException(t);
        }
    });
    return result;
}

From source file:org.thingsboard.rule.engine.util.EntitiesFieldsAsyncLoader.java

private static <T extends BaseData> ListenableFuture<EntityFieldsData> getAsync(ListenableFuture<T> future,
        Function<T, EntityFieldsData> converter) {
    return Futures.transformAsync(future, in -> in != null ? Futures.immediateFuture(converter.apply(in))
            : Futures.immediateFailedFuture(new RuntimeException("Entity not found!")));
}

From source file:com.orangerhymelabs.helenus.cassandra.view.ViewService.java

public ListenableFuture<View> create(View view) {
    ListenableFuture<Boolean> tableFuture = tables.exists(view.databaseName(), view.tableName());
    return Futures.transformAsync(tableFuture, new AsyncFunction<Boolean, View>() {
        @Override/* w w w. ja v a2 s  . c o  m*/
        public ListenableFuture<View> apply(Boolean exists) throws Exception {
            if (exists) {
                try {
                    ValidationEngine.validateAndThrow(view);
                    return views.create(view);
                } catch (ValidationException e) {
                    return Futures.immediateFailedFuture(e);
                }
            } else {
                return Futures.immediateFailedFuture(
                        new ItemNotFoundException("Table not found: " + view.tableName()));
            }
        }
    }, MoreExecutors.directExecutor());
}

From source file:com.spotify.metrics.remote.OkRemote.java

@Override
public ListenableFuture<Integer> post(String path, String shardKey, Map jsonObj) {
    if ((path.length() > 0) && (path.charAt(0) != '/')) {
        path = "/" + path;
    }// w ww.j a va2  s  .  c o  m
    final String jsonStr;
    try {
        jsonStr = mapper.writeValueAsString(jsonObj);
    } catch (JsonProcessingException e) {
        return Futures.immediateFailedFuture(new RuntimeException("Invalid json input"));
    }
    final String url = "http://" + host + ":" + port + path;
    final RequestBody body = RequestBody.create(JSON, jsonStr);

    final Request request = new Request.Builder().url(url).addHeader(Sharder.SHARD_KEY, shardKey)
            .addHeader(CONTENT_TYPE_KEY, CONTENT_TYPE_VALUE).post(body).build();
    final SettableFuture<Integer> result = SettableFuture.create();
    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            result.set(503);
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            result.set(response.code());
        }
    });
    return result;
}