I need to implement batch job to migrate datafrom one mongodb collection to another.
I want to make it in async way. Please look at the following example
private void runBatch() {
mongo.count("clients", new JsonObject(), countResult -> {
if (countResult.succeeded()) {
int count = countResult.result().intValue();
for (int i = 0; i < count; i =200) {
final int skip = i;
final int limit = skip 199;
// create new promise to process every 200 clints
vertx.executeBlocking(promise -> {
FindOptions options = new FindOptions();
options.setSkip(skip);
options.setLimit(limit);
options.setSort(new JsonObject().put("_id", 1));
mongo.findWithOptions("clients", query, options, findResult -> {
List<JsonObject> clients = findResult.result();
for (JsonObject client : clients) {
mongo.save("best_clients", client, saveResul -> {
// TODO add logging
});
}
});
}, result -> {
// TODO add logging
});
}
}
});
}
Inside executeBlocking
I'm running a lot of async methods
I'm really confise Should I run somewhere promise.complete()
?
Maybe My approach is incorrect and I shouldn't work inside promise with 200 object either pass one object for one promise?
CodePudding user response:
A promise is usually used to represent the eventual completion or failure of an asynchronous operation; then, based on the promise's successful completion or failure, some operation is being carried out. (logging, acknowledging that some operation was carried out etc)
It beats the purpose of using a promise if it's not completed or its value never used.
Also remember that the value of a promise, if not completed, could be set (possibly by another thread depending on code implementation) which could lead to unexpected behavior. So it's recommended to always complete promises.