Article directory
1 RequestOptions: Request method (synchronous/asynchronous)
1.1 Problem generation
When using the ES advanced client to send a request to the ES server, and using the synchronous request method to create the index library, there is a piece of code, which refers to the
RequestOptions.DEFAULT
synchronous call method after reference, and of course there will be asynchronous calls.
1.2 Problem Analysis
calling method | explain |
---|---|
Synchronously | ① When executing IndexRequest synchronously, the client must wait for the returned result IndexResponse; ② After receiving the IndexResponse, the client can continue to execute the code |
asynchronous mode | ① When executing IndexRequest asynchronously, the advanced client does not have to wait synchronously for the return of the request result, and can directly return the successful result of asynchronous interface execution to the interface calling method; ② To process the response result returned asynchronously or process the processing information caused during the execution of the request , the user needs to specify the listener ActionListener. If the request is successfully executed, the onRespose() method of ActionListener is called, and then the corresponding logic processing in the method is executed; if the request execution fails, the onFailure() of ActionListener is called for corresponding logic processing |
1.2.1 Synchronization method
Synchronously:
restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
1.2.1 Asynchronous mode
Asynchronous way:
estHighLevelClient.indexAsync(indexRequest, RequestOptions.DEFAULT, actionListener);
1.3 Problem Solving
1.3.1 Asynchronous mode code
/**
* 向es服务器发送请求,创建索引,请求方式:异步
*/
@Test
public void testCreateIndex2(){
//目标:向es服务器发送请求,创建索引,请求方式:异步
//1.创建request对象
IndexRequest indexRequest = new IndexRequest("hetol");
indexRequest.source(Constants.MAPPING,XContentType.JSON);
//3.通过ES高级客户端向ES服务器通信
ActionListener<IndexResponse> listener = new ActionListener<IndexResponse>() {
@Override
public void onResponse(IndexResponse indexResponse) {
System.out.println("执行成功");
}
@Override
public void onFailure(Exception e) {
System.out.println("执行失败"+e.getMessage());
}
};
client.indexAsync(indexRequest,RequestOptions.DEFAULT,listener);
}
1.3.2 Asynchronous mode code problem thrown
During the execution of this code, two problems were found:
① At the end of the code execution, it was found that the index library in ES was successfully created
② While the index library was successfully created, it was found thatsevere crop: The execution is successful. After the success, the logic enters the onResponse ( ) method. It is puzzling, why结果却是走了onFailure( )失败的方法,报错说意外断开连接,难道这个方法就是如此吗(执行完断开连接,可是onResponse( )方法里的逻辑不是也就不能走了)?
1.3.2.1 Throwing problem analysis
Idea: Look at the source code , 正在分析
if you have any insights, please leave a message and discuss in the comment area.
1.4 References
ES advanced client document operation combat
ES 6: restHighLevelClient source code