您的位置:首页 > 理论基础 > 计算机网络

FiberHttpClient Basic

2017-03-02 00:00 99 查看
摘要: FiberHttpClient is the fiber async transform of apache httpClient.

Fiber Async Transform

将任意同步操作换成Fiber Async

FiberAsync

FiberHttpClient

@Override
@Suspendable
protected final CloseableHttpResponse doExecute(final HttpHost target, final HttpRequest request, final HttpContext context) throws IOException, ClientProtocolException {
try {
for (int executionCount = 0;; executionCount++) {
try {
final HttpResponse response = new AsyncHttpReq() {
@Override
protected void requestAsync() {      //所有的代码就是在这里把 sync 转化为fiber async
client.execute(target, request, context, this);
}
}.run();
return new CloseableHttpResponseWrapper(response);
}
........ 这里省略
}

Configuration

值得一提的是,FiberHttpClient,其实是用的 CloseableHttpAsyncClient来进行IO操作,只是一开始调用的线程换成了fiber,可以执行更多的请求(比线程轻量),和执行其它的操作(fiber.get() or channel.receive() )。

public class FiberHttpClient extends CloseableHttpClient {
private final Log log = LogFactory.getLog(getClass());
private final CloseableHttpAsyncClient client;
private final HttpRequestRetryHandler httpRequestRetryHandler;

private DefaultConnectingIOReactor ioreactor;

public FiberHttpClient(CloseableHttpAsyncClient client) {
this(client, null, null);
}

public FiberHttpClient(CloseableHttpAsyncClient client, IOReactor ioreactor) {
this(client, null, ioreactor);
}

public FiberHttpClient(CloseableHttpAsyncClient client, HttpRequestRetryHandler httpRequestRetryHandler) {
this(client, httpRequestRetryHandler, null);
}

public FiberHttpClient(CloseableHttpAsyncClient client, HttpRequestRetryHandler httpRequestRetryHandler, IOReactor ioreactor) {
this.client = client;
this.httpRequestRetryHandler = httpRequestRetryHandler;
if (ioreactor != null && ioreactor instanceof DefaultConnectingIOReactor)
this.ioreactor = (DefaultConnectingIOReactor) ioreactor;
if (!client.isRunning())
client.start();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Fiber HttpClient