I'm not a Java developer but I'm trying to put together a quick start snippet to help Java users consume our service using code generated from .proto service definitions.

I've cobbled together some working code, drawing from the examples in grpc/grpc-java, but I don't feel comfortable with how the host and port parameters are being set, or with the null return from the createUser method.

I'd appreciate any feedback on how easy the code below would be to implement, and highlight any significant issues or improvements that could be made to improve performance and developer productivity.

Example User Client class:

package com.example.UserClient;

import io.grpc.ManagedChannel;
import io.grpc.StatusRuntimeException;
import io.grpc.testing.TestUtils;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NegotiationType;
import io.grpc.netty.NettyChannelBuilder;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;

import javax.net.ssl.SSLException;
import java.io.File;
import java.security.cert.X509Certificate;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.example.UserGrpc;
import com.example.UserGrpc.UserBlockingStub;
import com.example.UserOuterClass.NewUser;
import com.example.UserOuterClass.NewUserResponse;

public class UserClient {

  private static final Logger logger = Logger.getLogger(UserClient.class.getName());

  private static String mHost;
  private static int mPort;

  private final ManagedChannel channel;
  private final UserBlockingStub userStub;

  private static SslContext buildSslContext(String host, int port, String trustFile, String clientCertFile, String clientKeyFile) throws SSLException {
    mHost = host;
    mPort = port;
    return GrpcSslContexts.forClient()
                          .trustManager(new File(trustFile))
                          .keyManager(new File(clientCertFile), new File(clientKeyFile))

  public UserClient(String host, int port, String trustFile, String clientCertFile, String clientKeyFile) throws SSLException {
    this(buildSslContext(host, port, trustFile, clientCertFile, clientKeyFile));

  public UserClient(SslContext sslContext) throws SSLException {
    this(NettyChannelBuilder.forAddress(mHost, mPort)

  UserClient(ManagedChannel channel) {
    this.channel = channel;
    userStub = UserGrpc.newBlockingStub(channel);

  public NewUserResponse createUser(NewUser user) {
    NewUserResponse newUser;
    try {
      newUser = userStub.createUser(user);
      return newUser;
    } catch (StatusRuntimeException e) {
      logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
      return null;

  // Other method implementations....

  public void shutdown() throws InterruptedException {
    channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);

Consuming the class:

UserClient client;
try {
  client = new UserClient("api.example.com", 443, "ca.pem", "client.pem", "client-key.pem");
  NewUser user = NewUser.newBuilder()
                        // additional params...

  NewUserResponse response = client.createUser(user);

} catch(Exception e) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.