Google Protocol Buffers Java
I hope you are familiar with Google Protocol Buffers and now a days its quite popular in the industry. In this tutorial I will show how to user Google Protocol Buffer using Java to read and write messages.
Tool needed:
- Protocol buffer compiler which is already included in project download link
- JDK 1.8
- Eclipse any version
- Maven latest version
- Create Maven project name: GoogleProtoBuff and below is final structure:
- pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>GoogleProtoBuff</artifactId> <groupId>com.javahonk.googleprotobuff</groupId> <version>1.0-SNAPSHOT</version> <properties> <protobuf.version>2.6.1</protobuf.version> <protoc-jar.version>2.6.1.3</protoc-jar.version> </properties> <dependencies> <!-- google proto buff --> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.5</version> </dependency> </dependencies> </project>
- First thing you need to do is define message formats in a .proto file. In this example I have created three .proto file to understand better how to create it:
- addressbook.proto:
option java_package = "com.javahonk.proto"; option java_outer_classname = "AddressBookProtos"; message Person { required string name = 1; required int32 id = 2; optional string email = 3; enum PhoneType { MOBILE = 0; HOME = 1; WORK = 2; } message PhoneNumber { required string number = 1; optional PhoneType type = 2 [default = HOME]; } repeated PhoneNumber phone = 4; } message AddressBook { repeated Person person = 1; }
- javaHonkDateRangeRequest.proto:
option java_package = "com.javahonk.proto"; option java_outer_classname = "JavaHonkDateRangeRequestProto"; message JavaHonkDateRangeRequestInfo { required string startDate = 1; required string endDate = 2; } message CompositeJavaHonkDateRangeRequestInfo { repeated JavaHonkDateRangeRequestInfo javaHonkDateRangeRequestInfo = 1; }
- javaHonkTest.proto:
option java_package = "com.javahonk.proto"; option java_outer_classname = "JavaHonkTestInfoProto"; message JavaHonkTestInfo { required string key = 1; required string executedBy = 2; } message CompositeJavaHonkTestInfo { repeated JavaHonkTestInfo javaHonkTestInfo = 1; }
- Next you will have to generate java class out of it, You will need protocol butter compiler which you could download from here but if you download complete project from below download link you don’t need to do it as I have included in the project. Please user command mentioned in “readme.txt” file:
readme.txt:
#Generating java files for Google protobuff protoc.exe -I=./ --java_out=../../src/main/java ./message/addressbook.proto protoc.exe -I=./ --java_out=../../src/main/java ./message/javaHonkDateRangeRequest.proto protoc.exe -I=./ --java_out=../../src/main/java ./message/javaHonkTest.proto
- Open command prompt from and use above command individually to generate java file as sample below in my case:
C:\Users\JavaHonk\eclipse-jee-luna-R-win32\eclipse\workspace_DSPS_Version1_New2\GoogleProtoBuff\scripts\protobuff>protoc.exe -I=./ --java_out=../../sr c/main/java ./message/addressbook.proto
- Use all three command you will see file generate in below package (Don’t forget to refresh your project to see generate files):
- Now read and write Protocol butter object use below class:
- ConvertToByteAndObject.java:
package com.javahonk.proto.convert; import java.io.IOException; import java.util.List; import com.javahonk.proto.JavaHonkDateRangeRequestProto; import com.javahonk.proto.JavaHonkDateRangeRequestProto.CompositeJavaHonkDateRangeRequestInfo; import com.javahonk.proto.JavaHonkDateRangeRequestProto.JavaHonkDateRangeRequestInfo; public class ConvertToByteAndObject { public static void main(String[] args) throws IOException { //Build and convert object to byte array System.out.println(buildObjectWithGoogleProtoBuff().toByteArray()); //Fetch value from byte array and convert to real object convertedObjectWithValue(); } public static CompositeJavaHonkDateRangeRequestInfo buildObjectWithGoogleProtoBuff() { JavaHonkDateRangeRequestProto.CompositeJavaHonkDateRangeRequestInfo.Builder compositeJavaHonkDateRangeRequestInfo = JavaHonkDateRangeRequestProto.CompositeJavaHonkDateRangeRequestInfo.newBuilder(); JavaHonkDateRangeRequestInfo.Builder javaHonkDateRangeRequestInfo = JavaHonkDateRangeRequestInfo.newBuilder(); javaHonkDateRangeRequestInfo.setStartDate("20160628"); javaHonkDateRangeRequestInfo.setEndDate("20160630"); compositeJavaHonkDateRangeRequestInfo.addJavaHonkDateRangeRequestInfo(javaHonkDateRangeRequestInfo); compositeJavaHonkDateRangeRequestInfo.addJavaHonkDateRangeRequestInfo(javaHonkDateRangeRequestInfo); return compositeJavaHonkDateRangeRequestInfo.build(); } public static void convertedObjectWithValue() throws IOException { byte[] message = buildObjectWithGoogleProtoBuff().toByteArray(); List<JavaHonkDateRangeRequestInfo> compositeSwapResetInfoList = CompositeJavaHonkDateRangeRequestInfo .parseFrom(message).getJavaHonkDateRangeRequestInfoList(); for (JavaHonkDateRangeRequestInfo javaHonkDateRangeRequestInfo : compositeSwapResetInfoList) { System.out.println(javaHonkDateRangeRequestInfo.getEndDate()); } } }
- Above I have showed you message conversion only for one protocol message you could use same technique for another protocol message.
Download project: GoogleProtoBuff