使用Java中的Thrift进行异步请求

Mas*_*rat 10 java asynchronous thrift

我正在寻找一个如何使用Thrift在Java中创建异步请求的示例.看看生成的代码这似乎是可能的,但我找不到一个如何的例子.

以下是生成代码的示例,该代码表明存在异步接口:

...
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);
      }
    }
 ...
Run Code Online (Sandbox Code Playgroud)

关于如何使用它的任何指针?

小智 9

使用上面的界面进行这样的异步调用(代码提到了Cassandra,但很容易推广到你的应用程序):

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());
Run Code Online (Sandbox Code Playgroud)