Fiber Async Transform
将任意同步操作换成Fiber Async
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();
}