我们应该如何在Apache HTTPClient5实现中实现HttpAsyncClient的AsyncResponseC



HTTPClient5实现中HTTPsyncClient的接口发生了如下变化:

public interface HttpAsyncClient {
/**
* Initiates asynchronous HTTP request execution using the given context.
* <p>
* The request producer passed to this method will be used to generate
* a request message and stream out its content without buffering it
* in memory. The response consumer passed to this method will be used
* to process a response message without buffering its content in memory.
* <p>
* Please note it may be unsafe to interact with the context instance
* while the request is still being executed.
*
* @param <T> the result type of request execution.
* @param requestProducer request producer callback.
* @param responseConsumer response consumer callback.
* @param pushHandlerFactory the push handler factory. Optional and may be {@code null}.
* @param context HTTP context. Optional and may be {@code null}.
* @param callback future callback. Optional and may be {@code null}.
* @return future representing pending completion of the operation.
*/
<T> Future<T> execute(
AsyncRequestProducer requestProducer,
AsyncResponseConsumer<T> responseConsumer,
HandlerFactory<AsyncPushConsumer> pushHandlerFactory,
HttpContext context,
FutureCallback<T> callback);
}

我想要的是,在客户端上调用execute之后,我在回调中得到一个HTTPResponse对象,然后在它上调用我自己的反序列化程序。现在这能工作吗?我想同时支持HTTP/1.1和HTTP/2。

我可以为AsyncRequestProducer找到一个构建器,但我不确定如何实现AsyncReponsConsumer,更不确定该从哪些实现中选择?影响和区别是什么?有一些像

  1. AbstractBinResponseConsumer
  2. 摘要字符响应消费者
  3. 基本响应消费者
  4. 简单响应消费者

我还可以在中找到AsyncResponseConsumer的工厂

<dependency>
<groupId>com.github.ok2c.hc5</groupId>
<artifactId>hc5-async-json</artifactId>
<version>0.2.1</version>
</dependency>

我希望有人能弄清楚这些都是什么,什么时候应该使用?在实施任何事情之前,我想了解其影响。

TIA!

  1. 忽略Basic*Simple*的各种实现。它们适用于已知消息内容较小(<100KiB(的最基本、最简单的情况
  2. 如果您正在处理文本消息,请扩展AbstractCharAsyncEntityConsumerAbstractCharResponseConsumer
  3. 如果您正在处理二进制消息,请扩展AbstractBinAsyncEntityConsumerAbstractBinResponseConsumer
  4. 如果确实必须使用一些基于经典(InputStream/OutputStream(的消息处理代码并将消息处理委托给工作线程池,则扩展AbstractClassicEntityConsumerAbstractClassicServerExchangeHandler
  5. 如果希望以完全异步、事件驱动的模式处理JSON消息,请使用hc5-async-json

最新更新