Example usage for io.vertx.core CompositeFuture cause

List of usage examples for io.vertx.core CompositeFuture cause

Introduction

In this page you can find the example usage for io.vertx.core CompositeFuture cause.

Prototype

Throwable cause(int index);

Source Link

Document

Returns a cause of a wrapped future

Usage

From source file:io.engagingspaces.vertx.dataloader.DataLoader.java

License:Open Source License

/**
 * Dispatches the queued load requests to the batch execution function and returns a composite future of the result.
 * <p>/*from   w w w . j a  v a2  s .  c  o  m*/
 * If batching is disabled, or there are no queued requests, then a succeeded composite future is returned.
 *
 * @return the composite future of the queued load requests
 */
public CompositeFuture dispatch() {
    if (!loaderOptions.batchingEnabled() || loaderQueue.size() == 0) {
        return CompositeFuture.join(Collections.emptyList());
    }
    CompositeFuture batch = batchLoadFunction.load(loaderQueue.keySet());
    dispatchedQueues.put(batch, new LinkedHashMap<>(loaderQueue));
    batch.setHandler(rh -> {
        AtomicInteger index = new AtomicInteger(0);
        dispatchedQueues.get(batch).forEach((key, future) -> {
            if (batch.succeeded(index.get())) {
                future.complete(batch.resultAt(index.get()));
            } else {
                future.fail(batch.cause(index.get()));
            }
            index.incrementAndGet();
        });
        dispatchedQueues.remove(batch);
    });
    loaderQueue.clear();
    return batch;
}