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 com.eviac.blog.samples.thrift.server  // 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 AdditionService.java

Writing a service handler

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

Example service handler (AdditionServiceHandler.java)
 
package com.eviac.blog.samples.thrift.server;

import org.apache.thrift.TException;

public class AdditionServiceHandler implements AdditionService.Iface {

 @Override
 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 (MyServer.java)
package com.eviac.blog.samples.thrift.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...");
   server.serve();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 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 (AdditionClient.java)
package com.eviac.blog.samples.thrift.client;

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);
   transport.open();

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

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

   transport.close();
  } catch (TTransportException e) {
   e.printStackTrace();
  } catch (TException x) {
   x.printStackTrace();
  }
 }

}


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

9 comments :

  1. Awesome tutorial.. Keep it up!

    ReplyDelete
  2. Hi

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

    In the generated code AdditionService.java 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 http://www.slf4j.org/codes.html#multiple_bindings 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(AdditionService.java:330)
    at com.thrift.test.server.AdditionService$add_args.setN1(AdditionService.java:316)
    at com.thrift.test.server.AdditionService$Client.send_add(AdditionService.java:76)
    at com.thrift.test.server.AdditionService$Client.add(AdditionService.java:69)
    at com.thrift.test.client.AdditionClient.main(AdditionClient.java:25)


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

    Regards,
    AlthafHussain.

    ReplyDelete
  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!

      Delete
  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 https://github.com/hypertable/hypertable/tree/master/src/java/ThriftClient/org/hypertable/thrift. But It showing an error in BasicClientTest.java and ThriftClient.java (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.

    Thanks.

    ReplyDelete
  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

    ReplyDelete