Java Code Examples for io.grpc.examples.helloworld.GreeterGrpc#newBlockingStub()

The following examples show how to use io.grpc.examples.helloworld.GreeterGrpc#newBlockingStub() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HelloworldActivity.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Override
protected String doInBackground(String... params) {
  String host = params[0];
  String message = params[1];
  String portStr = params[2];
  int port = TextUtils.isEmpty(portStr) ? 0 : Integer.valueOf(portStr);
  try {
    channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext().build();
    GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channel);
    HelloRequest request = HelloRequest.newBuilder().setName(message).build();
    HelloReply reply = stub.sayHello(request);
    return reply.getMessage();
  } catch (Exception e) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    e.printStackTrace(pw);
    pw.flush();
    return String.format("Failed... : %n%s", sw);
  }
}
 
Example 2
Source File: StrictModeHelloworldActivity.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Override
protected String doInBackground(String... params) {
  String host = params[0];
  String message = params[1];
  String portStr = params[2];
  int port = TextUtils.isEmpty(portStr) ? 0 : Integer.valueOf(portStr);
  try {
    channel =
        OkHttpChannelBuilder.forAddress(host, port)
            .transportExecutor(new NetworkTaggingExecutor(0xFDD))
            .usePlaintext()
            .build();
    GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channel);
    HelloRequest request = HelloRequest.newBuilder().setName(message).build();
    HelloReply reply = stub.sayHello(request);
    return reply.getMessage();
  } catch (Exception e) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    e.printStackTrace(pw);
    pw.flush();
    return String.format("Failed... : %n%s", sw);
  }
}
 
Example 3
Source File: RetryingHelloWorldClient.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
/**
 * Construct client connecting to HelloWorld server at {@code host:port}.
 */
public RetryingHelloWorldClient(String host, int port, boolean enableRetries) {

  ManagedChannelBuilder<?> channelBuilder = ManagedChannelBuilder.forAddress(host, port)
      // Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
      // needing certificates.
      .usePlaintext();
  if (enableRetries) {
    Map<String, ?> serviceConfig = getRetryingServiceConfig();
    logger.info("Client started with retrying configuration: " + serviceConfig);
    channelBuilder.defaultServiceConfig(serviceConfig).enableRetry();
  }
  channel = channelBuilder.build();
  blockingStub = GreeterGrpc.newBlockingStub(channel);
  this.enableRetries = enableRetries;
}
 
Example 4
Source File: HelloworldActivity.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Override
protected String doInBackground(String... params) {
  String host = params[0];
  String message = params[1];
  String portStr = params[2];
  int port = TextUtils.isEmpty(portStr) ? 0 : Integer.valueOf(portStr);
  try {
    channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext().build();
    GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channel);
    HelloRequest request = HelloRequest.newBuilder().setName(message).build();
    HelloReply reply = stub.sayHello(request);
    return reply.getMessage();
  } catch (Exception e) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    e.printStackTrace(pw);
    pw.flush();
    return String.format("Failed... : %n%s", sw);
  }
}
 
Example 5
Source File: HelloWorldAltsClient.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
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();
  }
}
 
Example 6
Source File: SafeMethodCachingInterceptorTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void unsafeCallsAreNotCached() {
  GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channelToUse);

  HelloReply reply1 = stub.sayHello(message);
  HelloReply reply2 = stub.sayHello(message);

  assertNotEquals(reply1, reply2);
}
 
Example 7
Source File: DetailErrorSample.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
void blockingCall() {
  GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channel);
  try {
    stub.sayHello(HelloRequest.newBuilder().build());
  } catch (Exception e) {
    verifyErrorReply(e);
  }
}
 
Example 8
Source File: ErrorHandlingClient.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
void blockingCall() {
  GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channel);
  try {
    stub.sayHello(HelloRequest.newBuilder().setName("Bart").build());
  } catch (Exception e) {
    Status status = Status.fromThrowable(e);
    Verify.verify(status.getCode() == Status.Code.INTERNAL);
    Verify.verify(status.getDescription().contains("Eggplant"));
    // Cause is not transmitted over the wire.
  }
}
 
Example 9
Source File: CustomHeaderClient.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
/**
 * A custom client.
 */
private CustomHeaderClient(String host, int port) {
  originChannel = ManagedChannelBuilder.forAddress(host, port)
      .usePlaintext()
      .build();
  ClientInterceptor interceptor = new HeaderClientInterceptor();
  Channel channel = ClientInterceptors.intercept(originChannel, interceptor);
  blockingStub = GreeterGrpc.newBlockingStub(channel);
}
 
Example 10
Source File: CompressingHelloWorldClient.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
/** Construct client connecting to HelloWorld server at {@code host:port}. */
public CompressingHelloWorldClient(String host, int port) {
  channel = ManagedChannelBuilder.forAddress(host, port)
      .usePlaintext()
      .build();
  blockingStub = GreeterGrpc.newBlockingStub(channel);
}
 
Example 11
Source File: HeaderClientInterceptorTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void clientHeaderDeliveredToServer() throws Exception {
  // Generate a unique in-process server name.
  String serverName = InProcessServerBuilder.generateName();
  // Create a server, add service, start, and register for automatic graceful shutdown.
  grpcCleanup.register(InProcessServerBuilder.forName(serverName).directExecutor()
      .addService(ServerInterceptors.intercept(new GreeterImplBase() {}, mockServerInterceptor))
      .build().start());
  // Create a client channel and register for automatic graceful shutdown.
  ManagedChannel channel = grpcCleanup.register(
      InProcessChannelBuilder.forName(serverName).directExecutor().build());
  GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(
      ClientInterceptors.intercept(channel, new HeaderClientInterceptor()));
  ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);

  try {
    blockingStub.sayHello(HelloRequest.getDefaultInstance());
    fail();
  } catch (StatusRuntimeException expected) {
    // expected because the method is not implemented at server side
  }

  verify(mockServerInterceptor).interceptCall(
      Matchers.<ServerCall<HelloRequest, HelloReply>>any(),
      metadataCaptor.capture(),
      Matchers.<ServerCallHandler<HelloRequest, HelloReply>>any());
  assertEquals(
      "customRequestValue",
      metadataCaptor.getValue().get(HeaderClientInterceptor.CUSTOM_HEADER_KEY));
}
 
Example 12
Source File: DetailErrorSample.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
void blockingCall() {
  GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channel);
  try {
    stub.sayHello(HelloRequest.newBuilder().build());
  } catch (Exception e) {
    verifyErrorReply(e);
  }
}
 
Example 13
Source File: SafeMethodCachingInterceptorTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void unsafeCallsAreNotCached() {
  GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channelToUse);

  HelloReply reply1 = stub.sayHello(message);
  HelloReply reply2 = stub.sayHello(message);

  assertNotEquals(reply1, reply2);
}
 
Example 14
Source File: HelloWorldSimpleClient.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
/**
 * Construct client for accessing HelloWorld server using the existing channel.
 */
HelloWorldSimpleClient(ManagedChannel channel) {
    this.channel = channel;
    blockingStub = GreeterGrpc.newBlockingStub(channel);
}
 
Example 15
Source File: HelloWorldClientTls.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
/**
 * Construct client for accessing RouteGuide server using the existing channel.
 */
HelloWorldClientTls(ManagedChannel channel) {
    this.channel = channel;
    blockingStub = GreeterGrpc.newBlockingStub(channel);
}
 
Example 16
Source File: HelloWorldClientTls.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
/**
 * Construct client for accessing RouteGuide server using the existing channel.
 */
HelloWorldClientTls(ManagedChannel channel) {
    this.channel = channel;
    blockingStub = GreeterGrpc.newBlockingStub(channel);
}
 
Example 17
Source File: AuthClient.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
/**
 * Construct client for accessing GreeterGrpc server using the existing channel.
 */
AuthClient(CallCredentials callCredentials, ManagedChannel channel) {
  this.callCredentials = callCredentials;
  this.channel = channel;
  this.blockingStub = GreeterGrpc.newBlockingStub(channel);
}
 
Example 18
Source File: ClientCacheExampleActivity.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Override
protected String doInBackground(Object... params) {
  String host = (String) params[0];
  String message = (String) params[1];
  String portStr = (String) params[2];
  boolean useGet = (boolean) params[3];
  boolean noCache = (boolean) params[4];
  boolean onlyIfCached = (boolean) params[5];
  int port = TextUtils.isEmpty(portStr) ? 0 : Integer.valueOf(portStr);
  try {
    channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext().build();
    Channel channelToUse =
        ClientInterceptors.intercept(
            channel, SafeMethodCachingInterceptor.newSafeMethodCachingInterceptor(cache));
    HelloRequest request = HelloRequest.newBuilder().setName(message).build();
    HelloReply reply;
    if (useGet) {
      MethodDescriptor<HelloRequest, HelloReply> safeCacheableUnaryCallMethod =
          GreeterGrpc.getSayHelloMethod().toBuilder().setSafe(true).build();
      CallOptions callOptions = CallOptions.DEFAULT;
      if (noCache) {
        callOptions =
            callOptions.withOption(SafeMethodCachingInterceptor.NO_CACHE_CALL_OPTION, true);
      }
      if (onlyIfCached) {
        callOptions =
            callOptions.withOption(
                SafeMethodCachingInterceptor.ONLY_IF_CACHED_CALL_OPTION, true);
      }
      reply =
          ClientCalls.blockingUnaryCall(
              channelToUse, safeCacheableUnaryCallMethod, callOptions, request);
    } else {
      GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channelToUse);
      reply = stub.sayHello(request);
    }
    return reply.getMessage();
  } catch (Exception e) {
    Log.e(TAG, "RPC failed", e);
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    e.printStackTrace(pw);
    pw.flush();
    return String.format("Failed... : %n%s", sw);
  }
}
 
Example 19
Source File: ClientCacheExampleActivity.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Override
protected String doInBackground(Object... params) {
  String host = (String) params[0];
  String message = (String) params[1];
  String portStr = (String) params[2];
  boolean useGet = (boolean) params[3];
  boolean noCache = (boolean) params[4];
  boolean onlyIfCached = (boolean) params[5];
  int port = TextUtils.isEmpty(portStr) ? 0 : Integer.valueOf(portStr);
  try {
    channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext().build();
    Channel channelToUse =
        ClientInterceptors.intercept(
            channel, SafeMethodCachingInterceptor.newSafeMethodCachingInterceptor(cache));
    HelloRequest request = HelloRequest.newBuilder().setName(message).build();
    HelloReply reply;
    if (useGet) {
      MethodDescriptor<HelloRequest, HelloReply> safeCacheableUnaryCallMethod =
          GreeterGrpc.getSayHelloMethod().toBuilder().setSafe(true).build();
      CallOptions callOptions = CallOptions.DEFAULT;
      if (noCache) {
        callOptions =
            callOptions.withOption(SafeMethodCachingInterceptor.NO_CACHE_CALL_OPTION, true);
      }
      if (onlyIfCached) {
        callOptions =
            callOptions.withOption(
                SafeMethodCachingInterceptor.ONLY_IF_CACHED_CALL_OPTION, true);
      }
      reply =
          ClientCalls.blockingUnaryCall(
              channelToUse, safeCacheableUnaryCallMethod, callOptions, request);
    } else {
      GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channelToUse);
      reply = stub.sayHello(request);
    }
    return reply.getMessage();
  } catch (Exception e) {
    Log.e(TAG, "RPC failed", e);
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    e.printStackTrace(pw);
    pw.flush();
    return String.format("Failed... : %n%s", sw);
  }
}