Apache Thrift Hello World Sample

Thrift is one of the RPC frameworks that are widely used nowadays. It’s originally developed at Facebook and then open sourced under Apache Foundataion. It’s supported by major programming or scripting languages like Java, C++, Python, Ruby, etc. The typical use case is for building distributed systems, mostly in house.

Here is a sample code I wrote to get myself familiar with the Apache Thrift. It could take some time to set it up and get it running. The easiest way is probably to use Maven so that you don’t have to download the needed jar files. See the end for the pom.xml file.

Bothered by SLOW Web UI to manage vSphere? Want to manage ALL your VMware vCenters, AWS, Azure, Openstack, container behind a SINGLE pane of glass? Want to search, analyze, report, visualize VMs, hosts, networks, datastores, events as easily as Google the Web? Find out more about vSearch 3.0: the search engine for all your private and public clouds.

The first is the interface definition file called hello.thrift:

namespace java org.doublecloud.thriftdemo
 
service Hello {
  string hi();
}

The second file is the HelloImpl.java which implements the Hello.Iface interface generated from the above thrift interface definition (IDL).

package org.doublecloud.thriftdemo;
 
import org.apache.thrift.TException;
 
public class HelloImpl implements Hello.Iface
{
  public String hi() throws TException
  {
    return "Hello DoubleCloud.org";
  }
}

Now, let’s look at the ThriftServer.java, which should be run first when you trying this demo.

package org.doublecloud.thriftdemo;
 
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;
 
public class ThriftServer
{
  public static void main(String[] args)
  {
    try
    {
      TServerSocket serverTransport = new TServerSocket(7911);
      Hello.Processor processor = new Hello.Processor(new HelloImpl());
      TServer server = new TThreadPoolServer(
              new TThreadPoolServer.Args(serverTransport).processor(processor));
      System.out.println("Starting server on port 7911 ...");
      server.serve();
    } 
    catch (TTransportException e)
    {
      e.printStackTrace();
    }
  }
}

With the server is running, you can run the ThriftClient.java which connects to localhost where the ThriftServer.java runs.

package org.doublecloud.thriftdemo;
 
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
 
public class ThriftClient
{
 
  public static void main(String[] args)
  {
    try
    {
      TSocket transport = new TSocket("localhost", 7911);
      transport.open();
      TProtocol protocol = new TBinaryProtocol(transport);
 
      Hello.Client client = new Hello.Client(protocol);
 
      String result = client.hi();
      System.out.println("Return from server: " + result);
      transport.close();
    } 
    catch (TException e)
    {
      e.printStackTrace();
    }
  }
}

As mentioned earlier, you can use the Maven for easy build. My choice of IDE is now NetBeans which has a great Maven integration. You can just create a new Maven project there and use the following pom.xml as reference:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>org.doublecloud</groupId>
  <artifactId>ThriftDemo</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
 
  <name>ThriftDemo</name>
  <url>http://maven.apache.org</url>
 
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
 
  <pluginRepositories>
    <pluginRepository>
      <id>dtrott</id>
      <url>http://maven.davidtrott.com/repository</url>
    </pluginRepository>
  </pluginRepositories>
 
  <dependencies>
 
    <dependency>
      <groupId>org.apache.thrift</groupId>
      <artifactId>libthrift</artifactId>
      <version>0.9.1</version>
    </dependency>
 
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.5.8</version>
    </dependency>
 
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.5.2</version>
    </dependency>
 
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.14</version>
    </dependency>
 
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.1</version>
    </dependency>
 
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
 
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.thrift.tools</groupId>
        <artifactId>maven-thrift-plugin</artifactId>
        <version>0.1.10</version>
        <configuration>
          <thriftExecutable>C:\Dev\thrift-0.9.1\thrift-0.9.1.exe</thriftExecutable>
        </configuration>
        <executions>
          <execution>
            <id>thrift-sources</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
          <execution>
            <id>thrift-test-sources</id>
            <phase>generate-test-sources</phase>
            <goals>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
 
</project>

Note that you want to change the path to your own installation of Thrift. For the hello.thrift file, it should be placed in the thrift directory in parallel to the java under the src/main.

Have fun!

This entry was posted in Applications & Tools and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Trackbacks

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  • NEED HELP?


    My company has created products like vSearch ("Super vCenter"), vijavaNG APIs, EAM APIs, ICE tool. We also help clients with virtualization and cloud computing on customized development, training. Should you, or someone you know, need these products and services, please feel free to contact me: steve __AT__ doublecloud.org.

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.