Apache Thrift Java example


Goal of this example

Web services (SOAP and REST) are a very popular form of RPC (Remote Procedure Call) due to their advantages like, human readable format, extensibility, security, cross platform support, etc. This topic was well discussed previously in the CXF Example. On the other hand they come at a price : human readable format requires more CPU and bandwidth than a binary one. This overhead can be significant in applications making high amount of invocations, with big payload. The Facebook developers realized this and created a cross platform, binary RPC technology that as they say “Strive for performance first, elegance second”. This technology bacame an apache project and now used by high volume sites like Facebook, Evernote or last.fm. Here is a short comparison :

SOAP REST Thrift
Cross platform Yes Yes Yes
Well defined interface description Yes (WSDL) Limited (WADL) Yes (Thrift file)
Client code generated from interface definition Yes Limited Yes
Exception handling Built-in Manual Built-in
Human readable Moderate Yes No (for debug purpose XML encoding can be enabled)
Interface version handling No No Yes
Performance overhead HTTP + XML parsing HTTP + XML / JSON parsing Minimal

Technology Used

  • Thrift 0.9
  • JUnit 4.8.1

Structure of the example project

This example demonstrates how to create the following

  • Standalone Java SE Thrift server (in the java-se-server sub-project)
  • Java EE Thrift server (in the web-server sub-project)
  • Java client (in the client sub-project)
  • Thrift interface definitions
  • Interface versioning

Thrift is a contract first protocol, the interface definitions must be written manually, but there is an eclipse plugin that can help. Once the thrift files are written the client and the server code can be generated in many languages. This example uses Java both for client and server. Thift offers several options to create a Java SE standalone server in which case Thrift will manage thread handling. There is also a TServlet that allows to embed Thrift into any standard Java Web Server with minimal boilerplate code like this


public class UserServlet extends TServlet {

	private static final long serialVersionUID = -1479231841402200033L;

	public UserServlet() {

		super(new UserService.Processor<>(new UserServiceHandler()), new TCompactProtocol.Factory());

	}
}

This example also demonstrates the versioning of a Thrift service. There is a V1 and V2 thrift definition file and corresponding generated clients. The server uses the V2 definitions but it can be successfully called by the V1 client.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: