Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gRPC in Java - Blocking/nonblocking stubs

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.

like image 234
Dom Fraise Avatar asked Jul 28 '17 10:07

Dom Fraise


People also ask

What is gRPC blocking stub?

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.

How does gRPC work in Java?

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.

Is gRPC stub thread safe?

grpc. Channel is marked with @ThreadSafe annotation. Stubs are also thread-safe, which is why reconfiguration creates a new stub.

Does gRPC use Netty?

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.


2 Answers

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.

like image 79
Michael Krause Avatar answered Oct 24 '22 09:10

Michael Krause


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
like image 35
Uli Avatar answered Oct 24 '22 08:10

Uli