我想,我得从观测量+ Couchbase异步API脑爆炸:) 可能有人帮助我,好吗? 已经和批量操作打了几天,并且仍然无法理解如何通过适当的错误处理来完成批量操作。Couchbase异步批错误处理
比方说,我要更新Couchbase散装一些文件。 如果我使用同步API,它看起来像:
List< JsonDocument> items = getItems(1, 2, 3, 4, 5);
// getItems - some method which calls bucket.get() for specified keys
for (JsonDocument item : items) {
try {
try {
item.content().put("age", 42);
bucket.replace(item);
} catch (CASMismatchException e) {
// retry
bucket.get(item.id()).content().put("age", 42);
bucket.replace(item);
}
} catch (Exception e) {
// handle error which doesn't stop execution for other items
// for example, add item id to list of failed items in response
errorHandler.handleError(item.id(), e);
}
}
但这不是平行的,和文档说异步API更有效。 我不能理解的是,如何通过建立这样的观测量流量,我想:
Observable.from(items)
.flatMap(item -> {
item.content().put("age", 42);
return bucket.async().replace(item);
})
.onErrorResumeNext(error -> {
// what to do? return another observable which does retry logic above?
// how do I know what item has failed?
// I don't have ID of that item, nor I can extract it from passed Exception
// why onErrorResumeNext is getting called only once (if one item fails)
// and is not called for other items?
})
.subscribe(); // also need Subscriber with onError (otherwise there are warnings in log)
任何帮助将非常感激! 感谢
我想你最好需要通过Observable.create建立可观察与尝试捕捉然后直接重试,如果重试工作发出这个项目,如果没有则发出错误。 –