Wednesday, July 4, 2012

Apache Thrift with Java quickstart

Apache Thrift is a RPC framework founded by facebook and now it is an Apache project. Thrift lets you define data types and service interfaces in a language neutral definition file. That definition file is used as the input for the compiler to generate code for building RPC clients and servers that communicate over different programming languages. You can refer Thrift white paper also.

According to the official web site Apache Thrift is a,
software framework, for scalable cross-language services development, combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml and Delphi and other languages.
Image courtesy wikipedia

Installing Apache Thrift in Windows

Installation Thrift can be a tiresome process. But for windows the compiler is available as a prebuilt exe. Download thrift.exe and add it into your environment variables.

Writing Thrift definition file (.thrift file)

Writing the Thrift definition file becomes really easy once you get used to it. I found this tutorial quite useful to begin with.

Example definition file (add.thrift)
namespace java  // defines the namespace 

typedef i32 int  //typedefs to get convenient names for your types

service AdditionService {  // defines the service to add two numbers
        int add(1:int n1, 2:int n2), //defines a method

Compiling Thrift definition file

To compile the .thrift file use the following command.
thrift --gen <language> <Thrift filename>
For my example the command is,
thrift --gen java add.thrift
After performing the command, inside gen-java directory you'll find the source codes which is useful for building RPC clients and server. In my example it will create a java code called

Writing a service handler

Service handler class is required to implement the AdditionService.Iface interface.

Example service handler (

import org.apache.thrift.TException;

public class AdditionServiceHandler implements AdditionService.Iface {

 public int add(int n1, int n2) throws TException {
  return n1 + n2;

Writing a simple server

Following is an example code to initiate a simple thrift server. To enable the multithreaded server uncomment the commented parts of the example code.

Example server (

import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TServer.Args;
import org.apache.thrift.server.TSimpleServer;

public class MyServer {

 public static void StartsimpleServer(AdditionService.Processor<AdditionServiceHandler> processor) {
  try {
   TServerTransport serverTransport = new TServerSocket(9090);
   TServer server = new TSimpleServer(
     new Args(serverTransport).processor(processor));

   // Use this for a multithreaded server
   // TServer server = new TThreadPoolServer(new
   // TThreadPoolServer.Args(serverTransport).processor(processor));

   System.out.println("Starting the simple server...");
  } catch (Exception e) {
 public static void main(String[] args) {
  StartsimpleServer(new AdditionService.Processor<AdditionServiceHandler>(new AdditionServiceHandler()));


Writing the client

Following is an example java client code which consumes the service provided by AdditionService.

Example client code (

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;

public class AdditionClient {

 public static void main(String[] args) {

  try {
   TTransport transport;

   transport = new TSocket("localhost", 9090);;

   TProtocol protocol = new TBinaryProtocol(transport);
   AdditionService.Client client = new AdditionService.Client(protocol);

   System.out.println(client.add(100, 200));

  } catch (TTransportException e) {
  } catch (TException x) {


Run the server code( It should output following and will listen to the requests.
Starting the simple server...
Then run the client code( It should output following.


  1. Awesome tutorial.. Keep it up!

  2. Hi

    When I try to run the code, is showing the correct output...,
    but iis showing foll.exceptions:

    In the generated code in some places it shows redmarks, because of that only client is not running ....

    SLF4J: Class path contains multiple SLF4J bindings.
    SLF4J: Found binding in [jar:file:/C:/Users/althaf_hussain/Desktop/jars1/slf4j-log4j12-1.5.8.jar!/org/slf4j/impl/StaticLoggerBinder.class]
    SLF4J: Found binding in [jar:file:/C:/Users/althaf_hussain/Desktop/jars1/slf4j-simple-1.6.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
    SLF4J: See for an explanation.
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The method setBit(byte, int, boolean) is undefined for the type EncodingUtils

    at com.thrift.test.server.AdditionService$add_args.setN1IsSet(
    at com.thrift.test.server.AdditionService$add_args.setN1(
    at com.thrift.test.server.AdditionService$Client.send_add(
    at com.thrift.test.server.AdditionService$Client.add(
    at com.thrift.test.client.AdditionClient.main(

    Please help me out for this...,
    what thing i have missed.


  3. Replies
    1. I've got just one question. What happens to the exceptions that can be thrown by the java server? I've developed a thrift service procesing database data - and when I've got some bug in the server code, the server just stops this request and processes another one and no trace of the exception is available. Thanks in advance for any suggestion!

  4. Hi,

    This is really great tutorial. I got with this. I'm using Hypertable DB(NoSql). It uses Thrift. So I installed Apache-Thrift and Hypertable in my windows 7. Now I want to connect with the Hypertable DB using java code. I want one simple .java file to invoke the thrift , connecting to hypertable and other required things. I mean, from my java code itself everything should be done automatically like connecting db, creating namespace, tables, insert, delete, alter etc.,(like RDBMS type of operations) I took sample code from But It showing an error in and (In both import error, import org.hypertable.thriftgen.*; and othere errors). So I want you to help me, so that I can write a single class in java, that do all operations automatically without explicitly doing by us. Please reply as soon as possible.


  5. I appreciate that you produced this wonderful article to help us get more knowledge about this topic. I know, it is not an easy task to write such a big article in one day, I've tried that and I've failed. But, here you are, trying the big task and finishing it off and getting good comments and ratings. That is one hell of a job done!
    python course institute in bangalore
    python Course in bangalore
    python training institute in bangalore