10

I'm looking for an example of how to make an asynchronous request in Java using Thrift. Looking at the generated code this seems to be possible, but I can't find a single example of how.

Here is an example of generated code that suggest the existence of an Asynchronous interface:

...
AsyncIface {
    public static class Factory implements org.apache.thrift.async.TAsyncClientFactory<AsyncClient> {
      private org.apache.thrift.async.TAsyncClientManager clientManager;
      private org.apache.thrift.protocol.TProtocolFactory protocolFactory;
      public Factory(org.apache.thrift.async.TAsyncClientManager clientManager, org.apache.thrift.protocol.TProtocolFactory protocolFactory) {
        this.clientManager = clientManager;
        this.protocolFactory = protocolFactory;
      }
      public AsyncClient getAsyncClient(org.apache.thrift.transport.TNonblockingTransport transport) {
        return new AsyncClient(protocolFactory, clientManager, transport);
      }
    }
 ...

Any pointer on how to use it?

MasterScrat
  • 5,726
  • 11
  • 41
  • 69

2 Answers2

9

Use the above interface to make the async call like this (The code mentions Cassandra but will easily generalize to your application):

TNonblockingTransport transport = new TNonblockingSocket("127.0.0.1", 9160);
TAsyncClientManager clientManager = new TAsyncClientManager();
TProtocolFactory protocolFactory = new TBinaryProtocol.Factory();
Cassandra.AsyncClient client = new Cassandra.AsyncClient(protocolFactory, clientManager, transport);

Cassandra.method_call(parameters, new Callback());
Ishan
  • 126
  • 1
  • 3
1

You haven't given any context, so I'll give you the basic parts you'll need:

  • To perform an asynchronous call, you'll need make it in a thread
  • To get the result, you'll need some kind of call back

The following represents a basic example of these elements in play:

final MyClient client;  // Who will get a call back to the their sendResult() method when asynch call finished
ExecutorService executor = Executors.newSingleThreadExecutor(); // Handy way to run code in a thread
Runnable task = new Runnable() { 
    public void run() { // Where the "do the call" code sits
        int result = someService.call(someParamter);
        client.sendResult(result); // For example, expecting an int result
    }
};
executor.submit(task); // This scheduled the runnable to be run
Bohemian
  • 365,064
  • 84
  • 522
  • 658
  • 2
    This is would be a way to do it, but it seems that Thrift generates code specifically meant to implement an asynchronous client. I've edited my question to make it a bit clearer. – MasterScrat Nov 05 '11 at 21:56
  • 5
    @Bohemian, I know you were trying to help, but those "working examples" are for a synchronous client, not async. And the linked Wiki article is even less useful. – Andrew Swan Mar 15 '12 at 10:07
  • yes, this defeats our purpose as well. We found in simulation our request time went from 30 seconds to 3 seconds on 100k rows in a file upload if we simulate an async version so we don't hold up threads in our system. – Dean Hiller Apr 16 '13 at 19:26