pyVision
A Machine Learning and Signal Processing toolbox

Download .zip Download.tar.gz View on GitHub


Apache Thrift Basics with java application

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.

Installation

Pre Requisite software

Run the following command to install the pre-requisite softwares

sudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libboost-filesystem-dev libboost-thread-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev

Download Software

  1. Thrift software can be downloaded from http://thrift.apache.org/download
  2. Copy the downloaded file into the desired directory and untar the file
tar -xvf thrift-0.10.0.tar.gz

For an Ubuntu linux distribution you just need to go to the thrift directory and type:

./configure
make all
sudo ln -s /usr/local/lib/libthriftc.so /usr/lib/libthriftc.so
sudo ln -s /usr/local/lib/libthriftc.so.0 /usr/lib/libthriftc.so.0
sudo ln -s /usr/local/lib/libthrift-0.10.0.so /usr/lib/libthrift-0.10.0.so

This installs the thrift code generation and other tools.

To verify the install

thrift -version

You can use apache Thrift in the maven project by including the below pom dependency

<dependency>
  <groupId>org.apache.thrift</groupId>
  <artifactId>libthrift</artifactId>
  <version>0.10.0</version>
</dependency>

Writing a .thrift file

In a .thrift file you can define the services that your server will implement and that they will be called by any clients. The Thrift compiler will read this file and generate source code to be used from the servers and clients you will write.

The Thrift interface definition language (IDL) allows for the definition of Thrift Types. A Thrift IDL file is processed by the Thrift code generator to produce code for the various target languages to support the defined structs and services in the IDL file.

Services are defined using Thrift types. Definition of a service is semantically equivalent to defining an interface (or a pure virtual abstract class) in object oriented programming. The Thrift compiler generates fully functional client and server stubs that implement the interface.

A service consists of a set of named functions, each with a list of parameters and a return type.

A simple .thrift file with which we define the UserLogin RPC is as follows

namespace java com.pyvision

struct userLoginRequest
{
    1:string name,
    2:string password,
    3:string devicetoken
}

struct userLoginResponse
{
    1:string name,
    2:string loginToken
}


service loginRPC
{
        userLoginResponse multiply(1:userLoginResponse n1)
}

This defines a loginRPC RPC with input argument as userLoginRequest and output as userLoginResponse

Generating the java file

To generate source code from thrift file run the command

thrift --gen java Login.json

This will generate the java files for the thrift code

Server Code

 public class loginRPCImpl implements loginRPC.Iface 
     {

     @Override
     public userLoginResponse send(userLoginRequest request) throws TException {
            System.out.println("received RPC "+request.getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException ex) {
                Logger.getLogger(loginRPCImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
          userLoginResponse r = new userLoginResponse();
          r.setName(request.getName());
          r.setLoginToken("1234");
          System.out.println("sending back reply RPC "+request.getName());
          return r;
    }

    };

loginRPC.Iface is abstract class for server side RPC which needs to be implemented

We initialize the server as follows

                 loginRPCImpl rpc=new loginRPCImpl();
                
                loginRPC.Processor<loginRPC.Iface> processor = new loginRPC.Processor<loginRPC.Iface>(rpc);
								
								TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
								
								server.serve();
								

The client side code is as follows

TTransport transport;
loginRPC.Client client;
transport = new TSocket("localhost", 7914);
transport.open();

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

userLoginRequest user=new userLoginRequest();
user.setDevicetoken("device1");
user.setName("user"+count);
user.setPassword("1234");
						
userLoginResponse rr=client.send(user);

transport.close();

**Apache Thrift RPC Architecture **

In thrift architecture

  • Protocol layer : provides serialization and de-serialization support.
  • Transport layer : provides is responsible for IPC communication
  • Processor : Reads data from the input, processes the data throught the Handler specified by the user and then writes the data to the output.

The protocol and transport layer are part of the runtime library. This means that it is possible to define a service and change the protocol and transport without recompiling the code.

Reference

  • https://dzone.com/articles/apache-thrift-java-quickstart
  • http://thrift.apache.org/docs/install/debian
  • http://thrift.apache.org/static/files/thrift-20070401.pdf
  • http://jnb.ociweb.com/jnb/jnbJun2009.html
blog comments powered by Disqus