我正在寻找如何使用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);
}
}
...
关于如何使用它有什么建议吗?
发布于 2012-01-10 13:11:54
使用上面的接口像这样进行异步调用(代码提到了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());
发布于 2011-09-19 03:56:33
您还没有给出任何上下文,所以我将给出您需要的基本部分:
下面是这些元素的一个基本示例:
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
https://stackoverflow.com/questions/7465435
复制