使用 Elasticsearch 的 Java High Level Rest Client 收到对异步请求的响应后立即返回某些内容

ala*_*bid 2 java rest elasticsearch

我正在使用 Elasticsearch 的 Java 高级 Rest 客户端,我希望 createProfileDocument 方法在收到对异步请求的响应后立即返回某些内容(就像 return 语句在 onResponse 方法中一样),我已经解决了这个问题(下面的代码)但我相信有更好的方法来做到这一点,我在文档中没有找到。这是我的代码:

private IndexResponse response = null;

public String createProfileDocument(ProfileDocument document) throws Exception {
    UUID uuid = UUID.randomUUID();
    document.setId(uuid.toString());
    IndexRequest indexRequest = new IndexRequest("profiles", "doc", document.getId())
            .source(convertProfileDocumentToMap(document));
    ActionListener<IndexResponse> listener;
    listener = new ActionListener<IndexResponse>() {
        @Override
        public void onResponse(IndexResponse indexResponse) {
            response = indexResponse;
            //I want it to behave as if the return statement was here
        }

        @Override
        public void onFailure(Exception e) {
            e.printStackTrace();
        }
    };
    client.indexAsync(indexRequest, RequestOptions.DEFAULT, listener);
    //waiting for response, shouldn't be done this way
    while (response == null) {

    }
    IndexResponse responseClone = response;
    response = null;
    return responseClone.getResult().name().toString();
}
Run Code Online (Sandbox Code Playgroud)

Ben*_*ent 5

两个选项:切换到同一个调用的同步版本

IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
Run Code Online (Sandbox Code Playgroud)

或者,如果您想继续使用异步版本。您可以PlainActionFuturepackage org.elasticsearch.action.support;

PlainActionFuture<IndexResponse> future = new PlainActionFuture<>();
client.indexAsync(indexRequest, RequestOptions.DEFAULT, future);
IndexResponse response = future.actionGet();
Run Code Online (Sandbox Code Playgroud)