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.
a blocking/synchronous stub: this means that the RPC call waits for the server to respond, and will either return a response or raise an exception. a non-blocking/asynchronous stub that makes non-blocking calls to the server, where the response is returned asynchronously.
grpc; The first line tells the compiler what syntax is used in this file. By default, the compiler generates all the Java code in a single Java file. The second line overrides this setting, and everything will be generated in individual files.
grpc. Channel is marked with @ThreadSafe annotation. Stubs are also thread-safe, which is why reconfiguration creates a new stub.
gRPC comes with three Transport implementations: The Netty-based transport is the main transport implementation based on Netty. It is for both the client and the server. The OkHttp-based transport is a lightweight transport based on OkHttp.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With