Search in sources :

Example 6 with HelloReply

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

the class SafeMethodCachingInterceptorTest method safeCallsAreCached.

@Test
public void safeCallsAreCached() {
    HelloReply reply1 = ClientCalls.blockingUnaryCall(channelToUse, safeGreeterSayHelloMethod, CallOptions.DEFAULT, message);
    HelloReply reply2 = ClientCalls.blockingUnaryCall(channelToUse, safeGreeterSayHelloMethod, CallOptions.DEFAULT, message);
    assertSame(reply1, reply2);
}
Also used : HelloReply(io.grpc.examples.helloworld.HelloReply) Test(org.junit.Test)

Example 7 with HelloReply

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

the class HelloWorldClientTls 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 {
        response = blockingStub.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)

Example 8 with HelloReply

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

the class XdsHelloWorldClient 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 {
        response = blockingStub.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)

Example 9 with HelloReply

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

the class ErrorHandlingClient method run.

void run() throws Exception {
    // Port 0 means that the operating system will pick an available port to use.
    Server server = ServerBuilder.forPort(0).addService(new GreeterGrpc.GreeterImplBase() {

        @Override
        public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
            responseObserver.onError(Status.INTERNAL.withDescription("Eggplant Xerxes Crybaby Overbite Narwhal").asRuntimeException());
        }
    }).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) GreeterGrpc(io.grpc.examples.helloworld.GreeterGrpc) HelloReply(io.grpc.examples.helloworld.HelloReply)

Example 10 with HelloReply

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

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