Post navigation

Google Protocol Buffer Jetty Web Server Integration

Google Protocol Buffer Jetty Web Server Integration

You must have seen and worked with many web servers where you would have pass data from web page to back end servers. As this is common for any web application. Think if you get requirement to communicate machine to machine, immediately you will start googling the solution and unfortunately you won’t find any complete example as me when I was working on this requirement for one of my recent project.

My requirement was:

Write application in Java which will exposed as web service without using any sever

It should work same as any web service works

Data serialization over the wire should backward compatible

Performance should be faster as compare to other technology as of date

You should do very less boiler plate coding

Easy to understand

After researching many option finally I decided to use Jetty Web server with Google Protocol Buffers.

What is Jetty Web server: Jetty is Java HTTP Web server with Java Servlet container. You might have seen web Servers are usually associated with only serving documents, Jetty is often used for machine to machine communications, within big software frameworks. Jetty is free and open source project as part of Eclipse Foundation. The web server is used in products such as Apache ActiveMQ, Alfresco, Apache Geronimo, Apache Maven, Apache Spark, Google App Engine, Eclipse, FUSE, Twitter Streaming API etc.. Jetty is also server in open source projects such as Lift, Eucalyptus, Red5, Hadoop. Jetty supports latest Java Servlet API which include JSP support as well as protocols HTTP/2 and WebSocket.

What is Google Protocol Buffers: Protocol Buffers is a technique of serializing structured data into byte streaem. It is very useful in developing programs which communicate each other over on a wire or for storing the data. Method involves an interface description language that describes structure of data and Program which generates source code from that description for generating or parsing a stream of bytes that represents structured data.

Google designed and developed Protocol Buffers for use internally and has provided a code generator for multiple languages. Actually the design goals for Protocol Buffers emphasized simplicity and performance and it was designed to be smaller and faster than XML. It is very similar to Apache Thrift protocol (Facebook has used it) except that the public Protocol Buffers implementation does not include concrete RPC protocol stack to use for the defined services. It also provide backward compatibility.

Now lets jump on actual implementation:

Tools needed:

Eclipse latest version

JDK 1.8

Google Protocol Buffers exe which is already include in the project for download

Maven 3.2.5

Jetty dependent jars (already included in pom.xml)

Steps:

First create Maven project name: JettyGoogleProtoBuffIntegration below It shows complete project structure for reference. I have put completed project for download in the bottom:

<!--Support fortesting Spring applications with tools such asJUnit and

TestNG Thisartifact isgenerally always defined witha'test'scope for

the integration testing framework andunit testing stubs-->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-test</artifactId>

<version>${org.springframework.version}</version>

<scope>test</scope>

</dependency>

<!--Spring Aspects-->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-aspects</artifactId>

<version>${org.springframework.version}</version>

</dependency>

<!--Spring Tx-->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-tx</artifactId>

<version>${org.springframework.version}</version>

</dependency>

<!--Spring JMS-->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jms</artifactId>

<version>${org.springframework.version}</version>

</dependency>

<!--Log4j-->

<dependency>

<groupId>org.apache.logging.log4j</groupId>

<artifactId>log4j-api</artifactId>

<version>${org.apache.log4j.version}</version>

</dependency>

<dependency>

<groupId>org.apache.logging.log4j</groupId>

<artifactId>log4j-core</artifactId>

<version>${org.apache.log4j.version}</version>

</dependency>

<!--commons-io-->

<dependency>

<groupId>commons-io</groupId>

<artifactId>commons-io</artifactId>

<version>2.4</version>

</dependency>

</dependencies>

</project>

First create Google Protocol Buffers template and generate java classes. As you see above structure its inside script folder:

javaHonkTest.proto: This is protocol buffers template class with two string parameter:

1

2

3

4

5

6

7

8

9

10

11

12

13

option java_package="com.javahonk.proto";

option java_outer_classname="JavaHonkTestInfoProto";

messageJavaHonkTestInfo{

required stringkey=1;

required stringexecutedBy=2;

}

messageCompositeJavaHonkTestInfo{

repeated JavaHonkTestInfo javaHonkTestInfo=1;

}

I have included other sample class (addressbook.proto, javaHonkDateRangeRequest.proto) as well to better understand proto template but we will use in our example only JavaHonkTestInfoProto.

Now to generate java class from it use below command mentioned in readme.txt and you will have to execute this command from ..JettyApplication\scripts\protobuff because in this folder you have protoc.exe file which generate Java class:

Create interface ServerMessageProcessor.java and define one method so that any class who implements this will include it to process the request. If you want to use it then just create seperate classed in use it:

HTTPPostSendTestData.java: This is test client class which will post the data in byte array where data is created using Google Protocol Buffers and it will also process the response from server and prints out the output: