问题描述
我正在使用 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();
}
1楼
两个选项:切换到同一个调用的同步版本
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
或者,如果您想继续使用异步版本。
您可以使用PlainActionFuture
在package org.elasticsearch.action.support;
PlainActionFuture<IndexResponse> future = new PlainActionFuture<>();
client.indexAsync(indexRequest, RequestOptions.DEFAULT, future);
IndexResponse response = future.actionGet();
2楼
我更喜欢异步请求,尤其是对于长时间运行的工作(例如重新索引)。 并且异步方式更优雅地处理失败。 下面是一个例子:
PlainActionFuture<BulkByScrollResponse> future = new PlainActionFuture<>();
restHighLevelRestClientHelper.reindex(INDEX_NAME, NEW_INDEX_NAME, future);
try {
// onResponse
if (future.isDone()) {
// Set 1 hour as the limit, if time out throw exception.
BulkByScrollResponse response = future.actionGet(TimeValue.timeValueMinutes(60));
System.out.println("The request took " + response.getTook());
} else {
// onFailure
throw new Exception("Something wrong with the ES query.");
}
} catch(Exception e) {
throw new Exception("The request timed out.");
}