Apache Thrift Java Server and NodeJS Client

As an exercise for learning Apache Thrift I’m writing a simple API for Apache Jackrabit 3 using this software framework developed by Facebook.  Apache Thrift is useful for writing cross-language services for many languages such Java, Python, PHP, JavaScript and others.

Apache Jackrabit 3 is an implementation of Java Content Repository  and for now it can only be used through It’s Java API (it also has a REST interface I haven’t used),  so the idea is to reproduce more or less the Jackrabbit 3 API more specifically the “Oak API” with Apache Thrift so it can be used on NodeJS.

Thrift Java Server


For the java server I have used _TNonblockingServerSocket transport  and binary protocol, _basically we have to register the available service processors, for this I have used TMultiplexedProcessor.

 public void testServerServe() throws TTransportException {
     TMultiplexedProcessor processor = new TMultiplexedProcessor();
     processor.registerProcessor("TRootService", rootProcessor);
     processor.registerProcessor("TTreeService", treeProcessor);

public static void nonBlocking(TProcessor processor) throws TTransportException {
        TNonblockingServerSocket serverTransport = new TNonblockingServerSocket(9090);
        THsHaServer server = new THsHaServer(
                new THsHaServer.Args(serverTransport)
                .protocolFactory(new TBinaryProtocol.Factory(false, false)));
        System.out.println("Starting server on port 9090 ...");

Thrift JavaScript Client


After running our server, we can consume the registered services.

testJcrOakClient: function(test) {

        var connection = thrift.createConnection('localhost', 9090, {transport: thrift.TFramedTransport}),
        multiplexer = new  thrift.Multiplexer(),
        root =  multiplexer.createClient('TRootService',TRoot,connection);        

        connection.on('error', function(err) {
        var path = "/";
        root.getTree(path, function(error, tree) {
            if (error) {
            } else {
                test.ok(tree, 'Session has not returned a result');
                test.equal(tree.path, path, 'The path of the tree should be equal the required path /');



Please be aware that I’m using the master branch of Apache Thrift, “Multiplexer processor” is not available in 0.9.x branch for NodeJS. The multiplexer allows to register and consume more than one service on a server, otherwise you’ll need a server for each service.

All the code is available on GitHub




comments powered by Disqus