Search in sources :

Example 26 with HelloRequest

use of io.grpc.examples.helloworld.HelloRequest 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)

Example 27 with HelloRequest

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

the class DetailErrorSample method advancedAsyncCall.

/**
 * This is more advanced and does not make use of the stub.  You should not normally need to do
 * this, but here is how you would.
 */
void advancedAsyncCall() {
    ClientCall<HelloRequest, HelloReply> call = channel.newCall(GreeterGrpc.getSayHelloMethod(), CallOptions.DEFAULT);
    final CountDownLatch latch = new CountDownLatch(1);
    call.start(new ClientCall.Listener<HelloReply>() {

        @Override
        public void onClose(Status status, Metadata trailers) {
            Verify.verify(status.getCode() == Status.Code.INTERNAL);
            Verify.verify(trailers.containsKey(DEBUG_INFO_TRAILER_KEY));
            try {
                Verify.verify(trailers.get(DEBUG_INFO_TRAILER_KEY).equals(DEBUG_INFO));
            } catch (IllegalArgumentException e) {
                throw new VerifyException(e);
            }
            latch.countDown();
        }
    }, new Metadata());
    call.sendMessage(HelloRequest.newBuilder().build());
    call.halfClose();
    if (!Uninterruptibles.awaitUninterruptibly(latch, 1, TimeUnit.SECONDS)) {
        throw new RuntimeException("timeout!");
    }
}
Also used : Status(io.grpc.Status) VerifyException(com.google.common.base.VerifyException) ClientCall(io.grpc.ClientCall) HelloRequest(io.grpc.examples.helloworld.HelloRequest) Metadata(io.grpc.Metadata) HelloReply(io.grpc.examples.helloworld.HelloReply) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

HelloRequest (io.grpc.examples.helloworld.HelloRequest)27 HelloReply (io.grpc.examples.helloworld.HelloReply)20 StatusRuntimeException (io.grpc.StatusRuntimeException)8 GreeterGrpc (io.grpc.examples.helloworld.GreeterGrpc)8 ManagedChannel (io.grpc.ManagedChannel)6 Metadata (io.grpc.Metadata)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Status (io.grpc.Status)3 StreamObserver (io.grpc.stub.StreamObserver)3 AbstractVerticle (io.vertx.core.AbstractVerticle)3 Future (io.vertx.core.Future)3 JksOptions (io.vertx.core.net.JksOptions)3 VertxChannelBuilder (io.vertx.grpc.VertxChannelBuilder)3 VertxServer (io.vertx.grpc.VertxServer)3 Test (org.junit.Test)3 ClientCall (io.grpc.ClientCall)2 Server (io.grpc.Server)2 GreeterImplBase (io.grpc.examples.helloworld.GreeterGrpc.GreeterImplBase)2 GreeterStub (io.grpc.examples.helloworld.GreeterGrpc.GreeterStub)2 HttpVersion (io.vertx.core.http.HttpVersion)2