elasticsearch中的Rest模塊通過實現listener的方式,完成對請求的響應。
public interface ActionListener<Response> {
/**
* A response handler.
*/
void onResponse(Response response);
/**
* A failure handler.
*/
void onFailure(Throwable e);
}
ActionListener<Response>接口是對一般響應地抽象,只有正常響應和異常響應兩個方面,沒有任何實現——elasticsearch采用實現listener接口地方式實現異步響應,而具體對響應如何處理則看具體地實現,此處的Response可以是任意類型。
在看一看bulk請求:
void bulk(BulkRequest request, ActionListener<BulkResponse> listener);
和RestBulkAction的具體實現
client.bulk(bulkRequest, new RestBuilderListener<BulkResponse>(channel) {
@Override
public RestResponse buildResponse(BulkResponse response, XContentBuilder builder) throws Exception {
....
}
});
此處處理具體的BulkRequest,使用了具體的RestBuilderListener<BulkResponse>(channel)類,此類一定實現了ActionListener接口,此時的response是BulkResponse。
從這兩個類就可以看出ES采用接口注入地方式處理響應,這樣響應的具體格式,就可以在請求端實現。從buildResponse中就可以看到對BulkResponse地具體處理。
從下面的繼承結構,可以看出RestResponse被處理地一般過程。
一句話概況各個類的作用:
ActionListener:最一般的響應抽象
RestActionListener:實現一般處理過程的框架,具體實現留給子類實現
RestResponseListener:將消息的構建過程以及發送過程分開
RestBuilderListener:專注于響應結構地構建
注意到elasticsearch源碼的細微變化
/**
* An action listener that requires {@link #processResponse(Object)} to be implemented
* and will automatically handle failures.
*/
public abstract class RestActionListener<Response> implements ActionListener<Response> {
// we use static here so we won't have to pass the actual logger each time for a very rare case of logging
// where the settings don't matter that much
private static ESLogger logger = Loggers.getLogger(RestResponseListener.class);
protected final RestChannel channel;
protected RestActionListener(RestChannel channel) {
this.channel = channel;
}
//變成了final,固定了onResponse的處理框架,子類只需要關注正常response地處理
@Override
public final void onResponse(Response response) {
try {
processResponse(response);
} catch (Throwable t) {
onFailure(t);
}
}
protected abstract void processResponse(Response response) throws Exception;
@Override
public final void onFailure(Throwable e) {
try {
channel.sendResponse(new BytesRestResponse(channel, e));
} catch (Throwable e1) {
logger.error("failed to send failure response", e1);
}
}
}
/**
* A REST enabled action listener that has a basic onFailure implementation, and requires
* sub classes to only implement {@link #buildResponse(Object)}.
*/
public abstract class RestResponseListener<Response> extends RestActionListener<Response> {
protected RestResponseListener(RestChannel channel) {
super(channel);
}
//變成了final,固定了processResponse的處理框架,主要作用將buildResponse和sendResponse分開
@Override
protected final void processResponse(Response response) throws Exception {
channel.sendResponse(buildResponse(response));
}
/**
* Builds the response to send back through the channel.
*/
public abstract RestResponse buildResponse(Response response) throws Exception;
}
/**
* A REST action listener that builds an {@link XContentBuilder} based response.
*/
public abstract class RestBuilderListener<Response> extends RestResponseListener<Response> {
public RestBuilderListener(RestChannel channel) {
super(channel);
}
//固定buildResponse,使用XContentBuilder對Response進行構建
@Override
public final RestResponse buildResponse(Response response) throws Exception {
return buildResponse(response, channel.newBuilder());
}
/**
* Builds a response to send back over the channel.
*/
public abstract RestResponse buildResponse(Response response, XContentBuilder builder) throws Exception;
}
之后實現RestBuilderListener的子類,會具體實現buildResponse的過程。
RestChannel主要完成兩件事:
1.基于Request構建基于builder的輸出
2.發送response
具體elsticsearch是如何發送response的呢?請看接下來的分析。