Search in sources :

Example 11 with HelloReply

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

the class HelloWorldAltsClient method run.

private void run(String[] args) throws InterruptedException {
    parseArgs(args);
    ExecutorService executor = Executors.newFixedThreadPool(1);
    ManagedChannel channel = AltsChannelBuilder.forTarget(serverAddress).executor(executor).build();
    try {
        GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channel);
        HelloReply resp = stub.sayHello(HelloRequest.newBuilder().setName("Waldo").build());
        logger.log(Level.INFO, "Got {0}", resp);
    } finally {
        channel.shutdown();
        channel.awaitTermination(1, TimeUnit.SECONDS);
        // Wait until the channel has terminated, since tasks can be queued after the channel is
        // shutdown.
        executor.shutdown();
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ManagedChannel(io.grpc.ManagedChannel) GreeterGrpc(io.grpc.examples.helloworld.GreeterGrpc) HelloReply(io.grpc.examples.helloworld.HelloReply)

Example 12 with HelloReply

use of io.grpc.examples.helloworld.HelloReply 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 13 with HelloReply

use of io.grpc.examples.helloworld.HelloReply 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 14 with HelloReply

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

the class DetailErrorSample method run.

void run() throws Exception {
    Server server = ServerBuilder.forPort(0).addService(new GreeterGrpc.GreeterImplBase() {

        @Override
        public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
            Metadata trailers = new Metadata();
            trailers.put(DEBUG_INFO_TRAILER_KEY, DEBUG_INFO);
            responseObserver.onError(Status.INTERNAL.withDescription(DEBUG_DESC).asRuntimeException(trailers));
        }
    }).build().start();
    channel = ManagedChannelBuilder.forAddress("localhost", server.getPort()).usePlaintext().build();
    blockingCall();
    futureCallDirect();
    futureCallCallback();
    asyncCall();
    advancedAsyncCall();
    channel.shutdown();
    server.shutdown();
    channel.awaitTermination(1, TimeUnit.SECONDS);
    server.awaitTermination();
}
Also used : Server(io.grpc.Server) HelloRequest(io.grpc.examples.helloworld.HelloRequest) Metadata(io.grpc.Metadata) GreeterGrpc(io.grpc.examples.helloworld.GreeterGrpc) HelloReply(io.grpc.examples.helloworld.HelloReply)

Example 15 with HelloReply

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

the class CompressingHelloWorldClient method greet.

/**
 * Say hello to server.
 */
public void greet(String name) {
    logger.info("Will try to greet " + name + " ...");
    HelloRequest request = HelloRequest.newBuilder().setName(name).build();
    HelloReply response;
    try {
        // This enables compression for requests. Independent of this setting, servers choose whether
        // to compress responses.
        response = blockingStub.withCompression("gzip").sayHello(request);
    } catch (StatusRuntimeException e) {
        logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
        return;
    }
    logger.info("Greeting: " + response.getMessage());
}
Also used : HelloRequest(io.grpc.examples.helloworld.HelloRequest) StatusRuntimeException(io.grpc.StatusRuntimeException) HelloReply(io.grpc.examples.helloworld.HelloReply)

Aggregations

HelloReply (io.grpc.examples.helloworld.HelloReply)55 Test (org.junit.Test)30 HelloRequest (io.grpc.examples.helloworld.HelloRequest)20 StatusRuntimeException (io.grpc.StatusRuntimeException)8 GreeterGrpc (io.grpc.examples.helloworld.GreeterGrpc)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 Metadata (io.grpc.Metadata)5 Status (io.grpc.Status)5 GreeterFutureStub (io.grpc.examples.helloworld.GreeterGrpc.GreeterFutureStub)4 SpanCustomizer (brave.SpanCustomizer)3 ManagedChannel (io.grpc.ManagedChannel)3 Span (zipkin2.Span)3 TraceContext (brave.propagation.TraceContext)2 ClientCall (io.grpc.ClientCall)2 Server (io.grpc.Server)2 GreeterStub (io.grpc.examples.helloworld.GreeterGrpc.GreeterStub)2 HostnameGreeter (io.grpc.examples.hostname.HostnameGreeter)2 StreamObserver (io.grpc.stub.StreamObserver)2 ExecutionException (java.util.concurrent.ExecutionException)2 CurrentSpanCustomizer (brave.CurrentSpanCustomizer)1