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:

Google Protocol Buffer Jetty Web Server Integration

  • pom.xml where all project dependencies has been included:

  • 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:

  • 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:

  • Once you execute above command then refresh your project you will below folder structure generate with below classes:

Google Protocol Buffer Jetty Web Server Integration

  • Create server-context.xml for spring to load:

  • javahonk.properties file to keep all application specific properties:

  • Create class JettyService.java to load and start server on port 8080 which is mentioned in properties file and you can change it to any port:

  • Define MessageHandler.java class which will process request from client send back response:

  • 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:

  • JavaHonkProcessor.java is main class which process the request and send back response:

  • 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:

  • Finally create Java main JavaHonkMainApp.java class to load and start the application:

  • If everything setup correctly you will below on your console without any exception:

Google Protocol Buffer Jetty Web Server Integration

  • Now right click HTTPPostSendTestData.java which will post data to Jetty and process it response you will below responses:

Google Protocol Buffer Jetty Web Server Integration

Google Protocol Buffer Jetty Web Server Integration

Download code: JettyGoogleProtoBuffIntegration

Leave a Reply

Your email address will not be published. Required fields are marked *