How to use Apache Thrift with Google App Engine

I’m on holiday an played around with some stuff I want to learn. So I came across the question: “Can I use Thrift with the Google App Engine”.  I could not find clear answers nor useful examples. That’s why I decided to test it.

Short answer: yes, you can!

The full story:

The Google App Engine is great to run simple Servlets. But it has many limitations. One limitation is that you can not use any outbound connection on any port (on the experimental Managed VM are more option available). But most Thrift tutorials use a TSocketServer with a specific port. This seem not to work on the GAE.

But… a thrift server can be implemented very easy as Servlet.

import org.apache.thrift.server.TServlet;
import org.apache.thrift.protocol.TCompactProtocol;

public class SearchServlet extends TServlet {

public SearchServlet() {
  super(new SearchEanService.Processor<>(new SearchEanHandler()), new TCompactProtocol.Factory());

My example interface has just one function “string search(string abc)”.

import org.apache.thrift.TException;

public class SearchEanHandler implements SearchEanService.Iface {
    public void ping() throws TException {
    public String search(String ean) throws TException {
        return "found the fish";

The client uses THttpClient and looks like this.

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TTransportException;
import org.apache.thrift.transport.THttpClient;

public class GameExClient  {
    private void invoke() {
        THttpClient transport;
        try {
            transport  = new THttpClient("http://localhost:8080/search");
            TProtocol protocol = new TCompactProtocol(transport);

            SearchEanService.Client client = new SearchEanService.Client(protocol);

            String result ="search it");
            System.out.println("Add result: " + result);

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

    public static void main(String[] args) {
        GameExClient c = new GameExClient();


So we need not separate port. The thrift client just connects over the regular port with the servlet context path.

Let’s try it.
Start the local Google App Engine

mvn3 appengine:devserver

And start the client. (yes, I know it looks a bit ugly, but the demo was just created with maven and vim)

java -cp ".:../lib/libthrift-0.9.3.jar:../lib/slf4j-api-1.7.12.jar:../lib/httpclient-4.4.1.jar:../lib/httpcore-4.4.4.jar" ch/rcms/gameex/GameExClient
Add result: found the fish

Voila and it works.
So lets forget about heavy REST / JSON implementations do it with thrift 🙂