Java Generated Code Reference
Packages
For each service defined in a .proto file, the Java code generation produces aJava class. The class name is the service’s name suffixed by Grpc
. The packagefor the generated code is specified in the .proto file using the java_package
option.
For example, if ServiceName
is defined in a .proto file containing thefollowing:
package grpcexample;
option java_package = "io.grpc.examples";
Then the generated class will be io.grpc.examples.ServiceNameGrpc
.
If java_package
is not specified, the generated class will use the package
as specified in the .proto file. This should be avoided, as proto packagesusually do not begin with a reversed domain name.
Service Stub
The generated Java code contains an inner abstract class suffixed withImplBase
, such as ServiceNameImplBase
. This class defines one Java methodfor each method in the service definition. It is up to the service implementerto extend this class and implement the functionality of these methods. Withoutbeing overridden, the methods return an error to the client saying the method isunimplemented.
The signatures of the stub methods in ServiceNameImplBase
vary depending onthe type of RPCs it handles. There are four types of gRPC service methods:unary, server-streaming, client-streaming, and bidirectional-streaming.
Unary
The service stub signature for a unary RPC method unaryExample
:
public void unaryExample(
RequestType request,
StreamObserver<ResponseType> responseObserver)
Server-streaming
The service stub signature for a server-streaming RPC methodserverStreamingExample
:
public void serverStreamingExample(
RequestType request,
StreamObserver<ResponseType> responseObserver)
Notice that the signatures for unary and server-streaming RPCs are the same. Asingle RequestType
is received from the client, and the service implementationsends its response(s) by invoking responseObserver.onNext(ResponseType response)
.
Client-streaming
The service stub signature for a client-streaming RPC methodclientStreamingExample
:
public StreamObserver<RequestType> clientStreamingExample(
StreamObserver<ResponseType> responseObserver)
Bidirectional-streaming
The service stub signature for a bidirectional-streaming RPC methodbidirectionalStreamingExample
:
public StreamObserver<RequestType> bidirectionalStreamingExample(
StreamObserver<ResponseType> responseObserver)
The signatures for client and bidirectional-streaming RPCs are the same. Sincethe client can send multiple messages to the service, the service implementationis reponsible for returning a StreamObserver<RequestType>
instance. ThisStreamObserver
is invoked whenever additional messages are received from theclient.
Client Stubs
The generated class also contains stubs for use by gRPC clients to call methodsdefined by the service. Each stub wraps a Channel
, supplied by the user of thegenerated code. The stub uses this channel to send RPCs to the service.
gRPC Java generates code for three types of stubs: asynchronous, blocking, andfuture. Each type of stub has a corresponding class in the generated code, suchas ServiceNameStub
, ServiceNameBlockingStub
, and ServiceNameFutureStub
.
Asynchronous Stub
RPCs made via an asynchronous stub operate entirely through callbacks onStreamObserver
.
The asynchronous stub contains one Java method for each method from the servicedefinition.
A new asynchronous stub is instantiated via the ServiceNameGrpc.newStub(Channel channel)
static method.
Unary
The asynchronous stub signature for a unary RPC method unaryExample
:
public void unaryExample(
RequestType request,
StreamObserver<ResponseType> responseObserver)
Server-streaming
The asynchronous stub signature for a server-streaming RPC methodserverStreamingExample
:
public void serverStreamingExample(
RequestType request,
StreamObserver<ResponseType> responseObserver)
Client-streaming
The asynchronous stub signature for a client-streaming RPC methodclientStreamingExample
:
public StreamObserver<RequestType> clientStreamingExample(
StreamObserver<ResponseType> responseObserver)
Bidirectional-streaming
The asynchronous stub signature for a bidirectional-streaming RPC methodbidirectionalStreamingExample
:
public StreamObserver<RequestType> bidirectionalStreamingExample(
StreamObserver<ResponseType> responseObserver)
Blocking Stub
RPCs made through a blocking stub, as the name implies, block until the responsefrom the service is available.
The blocking stub contains one Java method for each unary and server-streamingmethod in the service definition. Blocking stubs do not support client-streamingor bidirectional-streaming RPCs.
A new blocking stub is instantiated via theServiceNameGrpc.newBlockingStub(Channel channel)
static method.
Unary
The blocking stub signature for a unary RPC method unaryExample
:
public ResponseType unaryExample(RequestType request)
Server-streaming
The blocking stub signature for a server-streaming RPC methodserverStreamingExample
:
public Iterator<ResponseType> serverStreamingExample(RequestType request)
Future Stub
RPCs made via a future stub wrap the return value of the asynchronous stub in aGrpcFuture<ResponseType>
, which implements thecom.google.common.util.concurrent.ListenableFuture
interface.
The future stub contains one Java method for each unary method in the servicedefinition. Future stubs do not support streaming calls.
A new future stub is instantiated via the ServiceNameGrpc.newFutureStub(Channel channel)
static method.
Unary
The future stub signature for a unary RPC method unaryExample
:
public ListenableFuture<ResponseType> unaryExample(RequestType request)
Codegen
Typically the build system handles creation of the gRPC generated code.
For protobuf-based codegen, you can put your .proto
files in the src/main/proto
and src/test/proto
directories along with an appropriate plugin.
A typicalprotobuf-maven-plugin configuration for generating gRPC and ProtocolBuffers code would look like the following:
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.4.1.Final</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.3.0:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.4.0:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Eclipse and NetBeans users should also look at os-maven-plugin
'sIDE documentation.
A typicalprotobuf-gradle-plugin configuration would look like the following:
apply plugin: 'java'
apply plugin: 'com.google.protobuf'
buildscript {
repositories {
mavenCentral()
}
dependencies {
// ASSUMES GRADLE 2.12 OR HIGHER. Use plugin version 0.7.5 with earlier
// gradle versions
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
}
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.2.0"
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.4.0'
}
}
generateProtoTasks {
all()*.plugins {
grpc {}
}
}
}
Bazel developers can use thejava_grpc_library
rule, typically as follows:
load("@grpc_java//:java_grpc_library.bzl", "java_grpc_library")
proto_library(
name = "helloworld_proto",
srcs = ["src/main/proto/helloworld.proto"],
)
java_proto_library(
name = "helloworld_java_proto",
deps = [":helloworld_proto"],
)
java_grpc_library(
name = "helloworld_java_grpc",
srcs = [":helloworld_proto"],
deps = [":helloworld_java_proto"],
)
Android developers please seethis for reference.
If you wish to invoke the protobuf plugin for gRPC Java directly,the command-line syntax is as follows:
$ protoc --plugin=protoc-gen-grpc-java \
--grpc-java_out="$OUTPUT_FILE" --proto_path="$DIR_OF_PROTO_FILE" "$PROTO_FILE"