Search in sources :

Example 1 with GreeterFutureStub

use of io.grpc.examples.helloworld.GreeterGrpc.GreeterFutureStub in project grpc-java by grpc.

the class ErrorHandlingClient method futureCallCallback.

void futureCallCallback() {
    GreeterFutureStub stub = GreeterGrpc.newFutureStub(channel);
    ListenableFuture<HelloReply> response = stub.sayHello(HelloRequest.newBuilder().setName("Maggie").build());
    final CountDownLatch latch = new CountDownLatch(1);
    Futures.addCallback(response, new FutureCallback<HelloReply>() {

        @Override
        public void onSuccess(@Nullable HelloReply result) {
        // Won't be called, since the server in this example always fails.
        }

        @Override
        public void onFailure(Throwable t) {
            Status status = Status.fromThrowable(t);
            Verify.verify(status.getCode() == Status.Code.INTERNAL);
            Verify.verify(status.getDescription().contains("Crybaby"));
            // Cause is not transmitted over the wire..
            latch.countDown();
        }
    }, directExecutor());
    if (!Uninterruptibles.awaitUninterruptibly(latch, 1, TimeUnit.SECONDS)) {
        throw new RuntimeException("timeout!");
    }
}
Also used : Status(io.grpc.Status) GreeterFutureStub(io.grpc.examples.helloworld.GreeterGrpc.GreeterFutureStub) HelloReply(io.grpc.examples.helloworld.HelloReply) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with GreeterFutureStub

use of io.grpc.examples.helloworld.GreeterGrpc.GreeterFutureStub in project grpc-java by grpc.

the class DetailErrorSample method futureCallCallback.

void futureCallCallback() {
    GreeterFutureStub stub = GreeterGrpc.newFutureStub(channel);
    ListenableFuture<HelloReply> response = stub.sayHello(HelloRequest.newBuilder().build());
    final CountDownLatch latch = new CountDownLatch(1);
    Futures.addCallback(response, new FutureCallback<HelloReply>() {

        @Override
        public void onSuccess(@Nullable HelloReply result) {
        // Won't be called, since the server in this example always fails.
        }

        @Override
        public void onFailure(Throwable t) {
            verifyErrorReply(t);
            latch.countDown();
        }
    }, directExecutor());
    if (!Uninterruptibles.awaitUninterruptibly(latch, 1, TimeUnit.SECONDS)) {
        throw new RuntimeException("timeout!");
    }
}
Also used : GreeterFutureStub(io.grpc.examples.helloworld.GreeterGrpc.GreeterFutureStub) HelloReply(io.grpc.examples.helloworld.HelloReply) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 3 with GreeterFutureStub

use of io.grpc.examples.helloworld.GreeterGrpc.GreeterFutureStub in project grpc-java by grpc.

the class DetailErrorSample method futureCallDirect.

void futureCallDirect() {
    GreeterFutureStub stub = GreeterGrpc.newFutureStub(channel);
    ListenableFuture<HelloReply> response = stub.sayHello(HelloRequest.newBuilder().build());
    try {
        response.get();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new RuntimeException(e);
    } catch (ExecutionException e) {
        verifyErrorReply(e.getCause());
    }
}
Also used : GreeterFutureStub(io.grpc.examples.helloworld.GreeterGrpc.GreeterFutureStub) HelloReply(io.grpc.examples.helloworld.HelloReply) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with GreeterFutureStub

use of io.grpc.examples.helloworld.GreeterGrpc.GreeterFutureStub in project grpc-java by grpc.

the class ErrorHandlingClient method futureCallDirect.

void futureCallDirect() {
    GreeterFutureStub stub = GreeterGrpc.newFutureStub(channel);
    ListenableFuture<HelloReply> response = stub.sayHello(HelloRequest.newBuilder().setName("Lisa").build());
    try {
        response.get();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new RuntimeException(e);
    } catch (ExecutionException e) {
        Status status = Status.fromThrowable(e.getCause());
        Verify.verify(status.getCode() == Status.Code.INTERNAL);
        Verify.verify(status.getDescription().contains("Xerxes"));
    // Cause is not transmitted over the wire.
    }
}
Also used : Status(io.grpc.Status) GreeterFutureStub(io.grpc.examples.helloworld.GreeterGrpc.GreeterFutureStub) HelloReply(io.grpc.examples.helloworld.HelloReply) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

GreeterFutureStub (io.grpc.examples.helloworld.GreeterGrpc.GreeterFutureStub)4 HelloReply (io.grpc.examples.helloworld.HelloReply)4 Status (io.grpc.Status)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutionException (java.util.concurrent.ExecutionException)2