gpt4 book ai didi

onFailure 中的 Elasticsearch 错误处理(异步请求)

转载 作者:行者123 更新时间:2023-12-03 02:15:51 27 4
gpt4 key购买 nike

例如,当您在 elasticsearch 中发出异步请求时,onFailure 会返回 Exception。如下所示处理异常是否合适。

            @Override
public void onFailure(Exception e) {
Throwable[] throwables = e.getSuppressed();
for(Throwable throwable: throwables){
if (throwable instanceof ElasticsearchException) {
throw new ElasticsearchException("message");
} else if (throwable instanceof IOException) {
try {
throw new IOException("message");
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
});

最佳答案

我们正在使用 Future interface处理异步请求,get 方法将抛出异常(如果有)。以下是使用重新索引请求作为示例的片段:

    PlainActionFuture<BulkByScrollResponse> future = new PlainActionFuture<>();
ReindexRequest request = new ReindexRequest();
request.setSourceIndices(sourceIndexName);
request.setDestIndex(destIndexName);

restHighLevelClient.reindexAsync(request, RequestOptions.DEFAULT, future);

try {
BulkByScrollResponse response = future.actionGet();
} catch (Exception e) {
log.error("The reindex job aborted: {}", ExceptionUtils.getRootCause(e));
}
这是 reference从接口(interface)抛出的异常类型。

关于onFailure 中的 Elasticsearch 错误处理(异步请求),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63616749/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com