Making Apache Thrift Selector Server Non-blocking

Recently I am doing lot of experiments with Apache Thrift. Interesting piece of software framework  for scalable cross-language services development, combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml and Delphi and other languages.

Documentation seems to very limited. I was trying to use TThreadedSelectorServer 

Typical usage of TThreadedSelectorServer I found is as below

TNonblockingServerTransport trans = new TNonblockingServerSocket(port);
TThreadedSelectorServer.Args args = new TThreadedSelectorServer.Args(trans);
args.transportFactory(new TFramedTransport.Factory());
args.protocolFactory(new TBinaryProtocol.Factory());
args.processor(your processor);
args.selectorThreads(4);
args.workerThreads(32);
TServer server = new TThreadedSelectorServer(args);
server.serve();

But this serve is blocking call and you cannot use it in your already available server. Or I do not know any other way to make it embedded.

Here is my solution to make it work .

Create a new class child of TThreadedSelectorServer and implement serve and stop as below and you are good to go.

public class MyTThreadedSelectorServer extends TThreadedSelectorServer {

  public MyTThreadedSelectorServer(Args args) {
    super(args);
  }

  public void serve() {
    if (!startThreads()) {
      return;
    }
    // start listening, or exit
    if (!startListening()) {
      return;
    }
    setServing(true);
  }

  public void stop() {
    setServing(false);
    super.stop();
  }

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s