Simple Thrift tutorial

I briefly explain here how to build a simple Thrift application in Java that returns the time on the server.

First, define the Thrift interface in file time.thrift:

# time.thrift
namespace java tserver.gen
typedef i64 Timestamp
service TimeServer {
   Timestamp time()
}

Generate the Java code:

thrift --gen java time.thrift

Import the generated code into your workspace. Now, we will implement the interface in the file TimeServerImpl.java:

package server;
import org.apache.thrift.TException;
import tserver.gen.*;
class TimeServerImpl implements TimeServer.Iface {
   @Override
   public long time() throws TException {
      long time = System.currentTimeMillis();
      System.out.println("time() called: " + time);
      return time;
   }
}

Let’s now create a Java server in file Server.java:

package server;
import java.io.IOException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TBinaryProtocol.Factory;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;
import tserver.gen.TimeServer;
public class Server {
   private void start(){
      try {
         TServerSocket serverTransport = new TServerSocket(7911);
         TimeServer.Processor processor = new TimeServer.Processor(new TimeServerImpl());
         Factory protFactory = new TBinaryProtocol.Factory(true, true);
         TServer server = new TThreadPoolServer(processor, serverTransport, protFactory);
         System.out.println("Starting server on port 7911 ...");
         server.serve();
      } catch (TTransportException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   public static void main(String args[]){
      Server srv = new Server();
      srv.start();
   }
}

And a Java client, TimeClient.java:

package client;
import java.net.SocketException;
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;
import tserver.gen.TimeServer.Client;
public class TimeClient {
   private void start(){
      TTransport transport;
      try {
         transport = new TSocket("localhost", 7911);
         TProtocol protocol = new TBinaryProtocol(transport);Client client = new Client(protocol);
         transport.open();
         long time = client.time();
         System.out.println("Time from server:" + time);
         transport.close();
      } catch (SocketException e) {
         e.printStackTrace();
      } catch (TTransportException e) {
         e.printStackTrace();
      } catch (TException e) {
         e.printStackTrace();
      }
   }

   public static void main(String[] args) {
      TimeClient c = new TimeClient();
      c.start();
   }
}

Start now the server, and run the client. Congratulation ! You first Thrift app is running🙂

2 thoughts on “Simple Thrift tutorial

Comments are closed.