Search in sources :

Example 16 with HelloReply

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

the class CustomHeaderClient method greet.

/**
 * A simple client method that like {@link io.grpc.examples.helloworld.HelloWorldClient}.
 */
private 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 17 with HelloReply

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

the class HedgingHelloWorldClient method greet.

/**
 * Say hello to server.
 */
public void greet(String name) {
    HelloRequest request = HelloRequest.newBuilder().setName(name).build();
    HelloReply response = null;
    StatusRuntimeException statusRuntimeException = null;
    long startTime = System.nanoTime();
    try {
        response = blockingStub.sayHello(request);
    } catch (StatusRuntimeException e) {
        failedRpcs.incrementAndGet();
        statusRuntimeException = e;
    }
    long latencyMills = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime);
    latencies.offer(latencyMills);
    if (statusRuntimeException == null) {
        logger.log(Level.INFO, "Greeting: {0}. Latency: {1}ms", new Object[] { response.getMessage(), latencyMills });
    } else {
        logger.log(Level.INFO, "RPC failed: {0}. Latency: {1}ms", new Object[] { statusRuntimeException.getStatus(), latencyMills });
    }
}
Also used : HelloRequest(io.grpc.examples.helloworld.HelloRequest) StatusRuntimeException(io.grpc.StatusRuntimeException) HelloReply(io.grpc.examples.helloworld.HelloReply)

Example 18 with HelloReply

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

the class RetryingHelloWorldClient method greet.

/**
 * Say hello to server in a blocking unary call.
 */
public void greet(String name) {
    HelloRequest request = HelloRequest.newBuilder().setName(name).build();
    HelloReply response = null;
    StatusRuntimeException statusRuntimeException = null;
    try {
        response = blockingStub.sayHello(request);
    } catch (StatusRuntimeException e) {
        failedRpcs.incrementAndGet();
        statusRuntimeException = e;
    }
    totalRpcs.incrementAndGet();
    if (statusRuntimeException == null) {
        logger.log(Level.INFO, "Greeting: {0}", new Object[] { response.getMessage() });
    } else {
        logger.log(Level.INFO, "RPC failed: {0}", new Object[] { statusRuntimeException.getStatus() });
    }
}
Also used : HelloRequest(io.grpc.examples.helloworld.HelloRequest) StatusRuntimeException(io.grpc.StatusRuntimeException) HelloReply(io.grpc.examples.helloworld.HelloReply)

Example 19 with HelloReply

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

the class SafeMethodCachingInterceptorTest method combinedResponseCacheControlDirectives_parsesWithoutError.

@Test
public void combinedResponseCacheControlDirectives_parsesWithoutError() throws Exception {
    cacheControlDirectives.add("max-age=1,no-store , no-cache");
    HelloReply reply1 = ClientCalls.blockingUnaryCall(channelToUse, safeGreeterSayHelloMethod, CallOptions.DEFAULT, message);
    HelloReply reply2 = ClientCalls.blockingUnaryCall(channelToUse, safeGreeterSayHelloMethod, CallOptions.DEFAULT, message);
    assertNotEquals(reply1, reply2);
    Truth.assertThat(cache.internalCache).isEmpty();
    Truth.assertThat(cache.removedKeys).isEmpty();
}
Also used : HelloReply(io.grpc.examples.helloworld.HelloReply) Test(org.junit.Test)

Example 20 with HelloReply

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

the class SafeMethodCachingInterceptorTest method responseNoTransformDirective_notCached.

@Test
public void responseNoTransformDirective_notCached() throws Exception {
    cacheControlDirectives.add("no-transform");
    HelloReply reply1 = ClientCalls.blockingUnaryCall(channelToUse, safeGreeterSayHelloMethod, CallOptions.DEFAULT, message);
    HelloReply reply2 = ClientCalls.blockingUnaryCall(channelToUse, safeGreeterSayHelloMethod, CallOptions.DEFAULT, message);
    assertNotEquals(reply1, reply2);
    Truth.assertThat(cache.internalCache).isEmpty();
    Truth.assertThat(cache.removedKeys).isEmpty();
}
Also used : HelloReply(io.grpc.examples.helloworld.HelloReply) Test(org.junit.Test)

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