11

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.

Dom Fraise
  • 360
  • 1
  • 4
  • 12

2 Answers2

16

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.

Michael Krause
  • 4,689
  • 1
  • 21
  • 25
1

The stub classes are indeed generated from the .proto file, and this should happen automatically with the protobuf-gradle-plugin. You extend the stub classes on the client side with any custom code to send your data to the server.

If you follow the instructions to clone the project from Github and build it, this should all happen automatically. Make sure to clone the v1.4.0 tag as they say, not master, you'd likely run into problems otherwise:

git clone -b v1.4.0 https://github.com/grpc/grpc-java.git
Uli
  • 2,828
  • 20
  • 23