主線流程源碼分析
常規(guī)使用
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
// 請(qǐng)求報(bào)文的信息
Request request = new Request.Builder().url("https://www.baidu.com").get().build();
// 連接request和response的橋梁
Call call = okHttpClient.newCall(request);
// 同一個(gè)call對(duì)象只能執(zhí)行一次enqueue方法
// 異步請(qǐng)求
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
MLog.e(TAG, "onFailure: " + e.toString());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
/*step 5:獲取響應(yīng)體*/
MLog.e(TAG, "onResponse: " + response.body());
}
});
// 同步請(qǐng)求
Response response = call.execute();
源碼解析
同步請(qǐng)求
RealCall類調(diào)用execute方法
@Override public Response execute() throws IOException {
synchronized (this) {
...
try {
// 保存同步請(qǐng)求,加入到同步請(qǐng)求執(zhí)行隊(duì)列中
client.dispatcher().executed(this);
// 通過(guò)攔截器鏈獲取到Response
Response result = getResponseWithInterceptorChain();
if (result == null) throw new IOException("Canceled");
return result;
} catch (IOException e) {
eventListener.callFailed(this, e);
throw e;
} finally {
// 移除同步請(qǐng)求
client.dispatcher().finished(this);
}
}
dispatcher作用:加入和移除同步請(qǐng)求。
異步請(qǐng)求
1.RealCall調(diào)用enqueue方法
client.dispatcher().enqueue(new AsyncCall(responseCallback));
/** 同步請(qǐng)求隊(duì)列 */
private final Deque<RealCall> runningSyncCalls = new ArrayDeque<>();
/**異步就緒,執(zhí)行隊(duì)列*/
private final Deque<AsyncCall> readyAsyncCalls = new ArrayDeque<>();
// 里面還會(huì)包含已經(jīng)被取消,但是未執(zhí)行完的請(qǐng)求
private final Deque<AsyncCall> runningAsyncCalls = new ArrayDeque<>();
dispatcher的方法
synchronized void enqueue(AsyncCall call) {
// runningAsyncCalls運(yùn)行隊(duì)列
// 同時(shí)運(yùn)行異步任務(wù)小于64,同時(shí)訪問(wèn)同一個(gè)服務(wù)器不超過(guò)5個(gè)
if (runningAsyncCalls.size() < maxRequests && runningCallsForHost(call) < maxRequestsPerHost) {
runningAsyncCalls.add(call);
// 線程池執(zhí)行AsyncCall耗時(shí)任務(wù)
executorService().execute(call);
} else {
// readyAsyncCalls等待隊(duì)列
readyAsyncCalls.add(call);
}
}
這兩個(gè)隊(duì)列是雙端隊(duì)列
AsyncCall 耗時(shí)任務(wù)
2.AsyncCall類
由上面可以看到RealCall的enqueue方法,會(huì)執(zhí)行new AsyncCall的對(duì)象,該對(duì)象繼承runnable,實(shí)現(xiàn)run方法,重寫(xiě)execute方法。
@Override protected void execute() {
boolean signalledCallback = false;
try {
// 責(zé)任鏈設(shè)計(jì)模式
Response response = getResponseWithInterceptorChain();
// 任務(wù)是否被取消
if (retryAndFollowUpInterceptor.isCanceled()) {
signalledCallback = true;
responseCallback.onFailure(RealCall.this, new IOException("Canceled"));
} else {
signalledCallback = true;
responseCallback.onResponse(RealCall.this, response);
}
} catch (IOException e) {
// signalledCallback為true,代表用戶造成的異常,在回調(diào)的方法中業(yè)務(wù)處理出現(xiàn)錯(cuò)誤
if (signalledCallback) {
// Do not signal the callback twice!
Platform.get().log(INFO, "Callback failure for " + toLoggableString(), e);
} else {
eventListener.callFailed(RealCall.this, e);
responseCallback.onFailure(RealCall.this, e);
}
} finally {
// 1.把請(qǐng)求從正在請(qǐng)求的異步隊(duì)列中移除
// 2.所用到的隊(duì)列是非線程安全的,為了考慮線程安全加了同步鎖
// 3.調(diào)整任務(wù)隊(duì)列
client.dispatcher().finished(this);
}
}
粗略梳理主線流程
異步:
創(chuàng)建okHttpClient,Request對(duì)象 --> 通過(guò)newCall()將Request轉(zhuǎn)成RealCall對(duì)象 --> 調(diào)用RealCall的enqueue()(不能重復(fù)被執(zhí)行) --> 傳入的Callback封裝成AsyncCall對(duì)象,AsyncCall是Runnable -> 在子線程中通過(guò)攔截器鏈獲取到最終的Response
同步:
將Request方法封裝成Call對(duì)象,真正實(shí)現(xiàn)類是RealCall - 直接調(diào)用攔截器鏈去獲得最終的結(jié)果
用到的設(shè)計(jì)模式
建造者設(shè)計(jì)模式
OkHttpClient對(duì)象創(chuàng)建。
責(zé)任鏈設(shè)計(jì)模式
通過(guò)攔截器鏈獲取到最終的Response進(jìn)行處理。