Understanding the Autogenerated Code
Learn about the classes generated by the protoc compiler, which will be used to create gRPC client and server.
In gRPC, the .proto
file serves as the contract defining the service methods and message types exchanged between the server and client. However, this file alone isn’t sufficient for communication. It needs to be compiled into programming language-specific classes. In gRPC, compiled Protobuf code is the backbone of communication. The compiled auto-generated classes serve as the building blocks for the gRPC server and client implementations. These classes are automatically generated by the Protocol Buffers compiler (protoc
) from the .proto
files. In this lesson, we’ll explore the significance of auto-generated classes in the gRPC server and client implementation and how they facilitate gRPC communication. Auto-generated classes for service methods and message types simplify development by providing a structured and type-safe way to interact with gRPC services.
Generating Java code from .proto
file
The ftp_service.proto
file contains the service, messages, and other definitions specific to our FTP service. To generate Java classes from this file, we need to run the protocol buffer compiler (protoc
). The POM file lists the protoc
compiler for Java, which will be used to build the project using the mvn install
command.
If you are following along on your local machine along as we advance towards other lessons, compile the updated module subsequently by executing the mvn install
command to make sure there are no errors.
When the code is compiled, a target
directory is created. We can see the protoc-3.19.2...
executable in the protoc-plugins
folder. Maven downloads this executable and uses it to read the ftp_service.proto
file to create Java classes. These auto-generated classes can be found in the generated-sources
folder. Since we have used the java_package
option, the code will be placed io.datajek.ftpservice.autogenerated
package inside the generated_sources
directory. The protocol buffer compiler will automatically create the io/datajek/ftpservice/autogenerated
directory structure.
Classes: The java_multiple_files
option means that the compiler will create a separate .java
files for each of the top-level message, enumeration, and service declared in the .proto
file. The classes
directory contains files named AddFileRequest.java
, FileChunk.java
, ...