OkHttp3.0流程解析

首先看基本使用

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("").build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
	@Override
	public void onFailure(Call call, IOException e) {

	}
	@Override
	public void onResponse(Call call, Response response) throws IOException {

	}
	});

1. OkHttpClient对象client调用内部newCall方法,传入request,得到一个Call对象:

@Override public Call newCall(Request request) {
    return new RealCall(this, request, false /* for web socket */);
  }

该方法返回一个RealCall对象

2. call实例调用enqueue方法传入回调:

@Override 
public void enqueue(Callback responseCallback) {
    synchronized (this) {
      if (executed) throw new IllegalStateException("Already Executed");
      executed = true;
    }
    captureCallStackTrace();
    client.dispatcher().enqueue(new AsyncCall(responseCallback));//重点
  }

从上面代码中可知,RealCall中的enqueue方法中主要任务是调用OkHttpClient对象中的dispatcher()方法返回一个dispatcher对象并调用该对象的enqueue(AsyncCall call)方法,

synchronized void enqueue(AsyncCall call) {
    if (runningAsyncCalls.size() < maxRequests && runningCallsForHost(call) < maxRequestsPerHost) {
      runningAsyncCalls.add(call);
      executorService().execute(call);
    } else {
      readyAsyncCalls.add(call);
    }
  }

其中,runningAsyncCalls、readyAsyncCalls为存储类型为AsyncCall的Deque

/** Ready async calls in the order they'll be run. */
	private final Deque<AsyncCall> readyAsyncCalls = new ArrayDeque<>();

/** Running asynchronous calls. Includes canceled calls that haven't finished yet. */
	private final Deque<AsyncCall> runningAsyncCalls = new ArrayDeque<>();

/** Running synchronous calls. Includes canceled calls that haven't finished yet. */
	private final Deque<RealCall> runningSyncCalls = new ArrayDeque<>();

executorService()方法返回值为一个核心线程数为0的线程池

public synchronized ExecutorService executorService() {
    if (executorService == null) {
      executorService = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60, TimeUnit.SECONDS,
          new SynchronousQueue<Runnable>(), Util.threadFactory("OkHttp Dispatcher", false));
    }
    return executorService;
  }

执行executorService().execute(call);将AsyncCall(RealCall类中的实现了Runnable接口的内部类)对象加入到线程池中的的HashSet(存储类型为Worker)中,并执行Worker对象中的thread,从而执行AsyncCall对象中的execute方法

3. 执行AsyncCall对象中的execute方法

@Override protected void execute() {
      boolean signalledCallback = false;
      try {
        Response response = getResponseWithInterceptorChain();
        //通过retryAndFollowUpInterceptor拦截器中的isCanceled状态来确定是回调onFailure还是onResponse
        if (retryAndFollowUpInterceptor.isCanceled()) {
          signalledCallback = true;
          //responseCallback是开始传入的Callback
          responseCallback.onFailure(RealCall.this, new IOException("Canceled"));
        } else {
          signalledCallback = true;
          responseCallback.onResponse(RealCall.this, response);
        }
      } catch (IOException e) {
        if (signalledCallback) {
          // Do not signal the callback twice!
          Platform.get().log(INFO, "Callback failure for " + toLoggableString(), e);
        } else {
          responseCallback.onFailure(RealCall.this, e);
        }
      } finally {
        client.dispatcher().finished(this);
      }
    }
  }

在execute方法中首先通过**getResponseWithInterceptorChain()**方法生成Response 对象

Response getResponseWithInterceptorChain() throws IOException {
    // Build a full stack of interceptors.
    List<Interceptor> interceptors = new ArrayList<>();
    interceptors.addAll(client.interceptors());
    //负责失败重连以及重定向
    interceptors.add(retryAndFollowUpInterceptor);
    //用户构造的请求->发送到服务器的请求;服务器返回的响应->用户友好的响应
    interceptors.add(new BridgeInterceptor(client.cookieJar()));
    //读取返回直接返回、更新缓存
    interceptors.add(new CacheInterceptor(client.internalCache()));
    //负责和服务器建立连接
    interceptors.add(new ConnectInterceptor(client));
    if (!forWebSocket) {
    //配置OkHttpClient中设置的List<Interceptor>
      interceptors.addAll(client.networkInterceptors());
    }
    //向服务器发送请求数据;从服务器读取相应数据
    interceptors.add(new CallServerInterceptor(forWebSocket));

    Interceptor.Chain chain = new RealInterceptorChain(
        interceptors, null, null, null, 0, originalRequest);
    return chain.proceed(originalRequest);
  }

4. 上面方法最后生成RealInterceptorChain对象并执行proceed方法(根据interceptors 的list顺序依次链式执行)
首先看RealInterceptorChain类构造方法

public RealInterceptorChain(List<Interceptor> interceptors, StreamAllocation streamAllocation,
      HttpCodec httpCodec, RealConnection connection, int index, Request request) {
    this.interceptors = interceptors;//拦截器集合
    this.connection = connection;//连接
    this.streamAllocation = streamAllocation;
    this.httpCodec = httpCodec;
    this.index = index;//拦截器索引
    this.request = request;
  }

重点看proceed方法

public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec,
      RealConnection connection) throws IOException {
...
...
    // Call the next interceptor in the chain.
    RealInterceptorChain next = new RealInterceptorChain(
        interceptors, streamAllocation, httpCodec, connection, index + 1, request);
    Interceptor interceptor = interceptors.get(index);
    Response response = interceptor.intercept(next);

...
...
    return response;
  }

由上述源码可知,proceed方法中会用本次执行的index加1去生成新的RealInterceptorChain 对象,然后在执行本次拦截器的intercept(Chain chain)方法时将其传递进去,在每个interceptor对象的intercept(Chain chain)方法中中都会调用chain.proceed()方法,从而达到链式调用的目的。

5. 各个拦截器的作用

RetryAndFollowUpInterceptor:负责失败重连以及重定向

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值