Search in sources :

Example 1 with GreeterStub

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

the class ErrorHandlingClient method asyncCall.

void asyncCall() {
    GreeterStub stub = GreeterGrpc.newStub(channel);
    HelloRequest request = HelloRequest.newBuilder().setName("Homer").build();
    final CountDownLatch latch = new CountDownLatch(1);
    StreamObserver<HelloReply> responseObserver = new StreamObserver<HelloReply>() {

        @Override
        public void onNext(HelloReply value) {
        // Won't be called.
        }

        @Override
        public void onError(Throwable t) {
            Status status = Status.fromThrowable(t);
            Verify.verify(status.getCode() == Status.Code.INTERNAL);
            Verify.verify(status.getDescription().contains("Overbite"));
            // Cause is not transmitted over the wire..
            latch.countDown();
        }

        @Override
        public void onCompleted() {
        // Won't be called, since the server in this example always fails.
        }
    };
    stub.sayHello(request, responseObserver);
    if (!Uninterruptibles.awaitUninterruptibly(latch, 1, TimeUnit.SECONDS)) {
        throw new RuntimeException("timeout!");
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) Status(io.grpc.Status) GreeterStub(io.grpc.examples.helloworld.GreeterGrpc.GreeterStub) HelloRequest(io.grpc.examples.helloworld.HelloRequest) CountDownLatch(java.util.concurrent.CountDownLatch) HelloReply(io.grpc.examples.helloworld.HelloReply)

Example 2 with GreeterStub

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

the class DetailErrorSample method asyncCall.

void asyncCall() {
    GreeterStub stub = GreeterGrpc.newStub(channel);
    HelloRequest request = HelloRequest.newBuilder().build();
    final CountDownLatch latch = new CountDownLatch(1);
    StreamObserver<HelloReply> responseObserver = new StreamObserver<HelloReply>() {

        @Override
        public void onNext(HelloReply value) {
        // Won't be called.
        }

        @Override
        public void onError(Throwable t) {
            verifyErrorReply(t);
            latch.countDown();
        }

        @Override
        public void onCompleted() {
        // Won't be called, since the server in this example always fails.
        }
    };
    stub.sayHello(request, responseObserver);
    if (!Uninterruptibles.awaitUninterruptibly(latch, 1, TimeUnit.SECONDS)) {
        throw new RuntimeException("timeout!");
    }
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) GreeterStub(io.grpc.examples.helloworld.GreeterGrpc.GreeterStub) HelloRequest(io.grpc.examples.helloworld.HelloRequest) CountDownLatch(java.util.concurrent.CountDownLatch) HelloReply(io.grpc.examples.helloworld.HelloReply)

Aggregations

GreeterStub (io.grpc.examples.helloworld.GreeterGrpc.GreeterStub)2 HelloReply (io.grpc.examples.helloworld.HelloReply)2 HelloRequest (io.grpc.examples.helloworld.HelloRequest)2 StreamObserver (io.grpc.stub.StreamObserver)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Status (io.grpc.Status)1