Java中Thrift的异步请求

发布于 2024-12-05 11:58:27 字数 861 浏览 2 评论 0原文

我正在寻找如何使用 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);
      }
    }
 ...

有关于如何使用它的指针吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陈独秀 2024-12-12 11:58:27

使用上面的接口进行异步调用,如下所示(代码提到了 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());

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());
坠似风落 2024-12-12 11:58:27

您还没有给出任何上下文,所以我将为您提供您需要的基本部分:

  • 要执行异步调用,您需要在线程中进行调用
  • 要获取结果,您需要某种调用下面

展示了这些元素的基本示例:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文