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

https://github.com/ottogiron/jcr-oak-rpc/blob/development/jcr-oak-rpc-services/src/test/java/org/jumlabs/jcr/oak/rpc/api/JcrOakThritServerTests.java

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.

@Test
 public void testServerServe() throws TTransportException {
    //simple(sessionProcesor);
     TMultiplexedProcessor processor = new TMultiplexedProcessor();
     processor.registerProcessor("TRootService", rootProcessor);
     processor.registerProcessor("TTreeService", treeProcessor);
      nonBlocking(processor);
}

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

Thrift JavaScript Client

https://github.com/ottogiron/jcr-oak-rpc/blob/development/nodejs/jcr-oak-api/test/jcr-oak-rpc-api-nodejs_test.js

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) {
            console.error(err);
            test.done();
        });
        var path = "/";
        root.getTree(path, function(error, tree) {
            if (error) {
                console.log(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 /');
                console.log(tree);
                connection.end();
            }

            test.done();
        });

    }

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

https://github.com/ottogiron/jcr-oak-rpc

 

 


comments powered by Disqus