gRPC in Java - Blocking/nonblocking stubs

Dom Fraise picture Dom Fraise · Jul 28, 2017 · Viewed 11.4k times · Source

I am attempting to create a java grpc client to communicate with a server in go. I am new to grpc so am following this tutorial gRPC Java Tutorial. In these examples they refer to blocking and nonblocking stubs which they appear to import from elsewhere in their github.

import io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideBlockingStub;
import io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideStub;
...
...    
blockingStub = RouteGuideGrpc.newBlockingStub(channel);
asyncStub = RouteGuideGrpc.newStub(channel);

However I cannot find these classes in their repo. I am still hazy on exactly what they are for, should they have been produced when compiling the .proto file? Any help/pointers would be appreciated. Thanks.

Answer

Michael Krause picture Michael Krause · Jul 28, 2017

The grpc stub classes are generated when you run the protoc compiler and it finds a service declaration in your proto file. The stub classes are the API your client uses to make rpc calls on the service endpoint.

These stubs come in two flavors: blocking and async.

Blocking stubs are synchronous (block the currently running thread) and ensure that the rpc call invoked on it doesn't return until it returns a response or raises an exception. Care should be taken not to invoke an rpc on a blocking stub from the UI thread as this will result in an unresponsive/janky UI.

Asynchronous stubs make non-blocking rpc calls where the response is returned asynchronously via a StreamObserver callback object.

For more information, refer to the grpc documentation regarding stubs here.