FiberHttpClient Basic

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();
    }



转载于:https://my.oschina.net/tigerlene/blog/849560

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值