我正在寻找一个如何使用Thrift在Java中发出异步请求的示例。查看生成的代码,这似乎是可行的,但我找不到单个示例。

这是生成的代码示例,表明存在异步接口(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);
      }
    }
 ...

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

最佳答案

使用上面的接口(interface)进行这样的异步调用(代码提到了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());

07-26 02:59