Java Code Examples for io.vertx.core.CompositeFuture#setHandler()

The following examples show how to use io.vertx.core.CompositeFuture#setHandler() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DataLoader.java    From vertx-dataloader with Apache License 2.0 6 votes vote down vote up
/**
 * Dispatches the queued load requests to the batch execution function and returns a composite future of the result.
 * <p>
 * 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;
}
 
Example 2
Source File: DataLoaderTest.java    From vertx-dataloader with Apache License 2.0 6 votes vote down vote up
@Test
public void should_Support_loading_multiple_keys_in_one_call() {
    AtomicBoolean success = new AtomicBoolean();
    DataLoader<Integer, Integer> identityLoader = new DataLoader<>(keys ->
            CompositeFuture.join(keys.stream()
                    .map(Future::succeededFuture)
                    .collect(Collectors.toCollection(ArrayList::new))));

    CompositeFuture futureAll = identityLoader.loadMany(asList(1, 2));
    futureAll.setHandler(rh -> {
        assertThat(rh.result().size(), is(2));
        success.set(rh.succeeded());
    });
    identityLoader.dispatch();
    await().untilAtomic(success, is(true));
    assertThat(futureAll.list(), equalTo(asList(1, 2)));
}
 
Example 3
Source File: DataLoaderTest.java    From vertx-dataloader with Apache License 2.0 6 votes vote down vote up
@Test
public void should_Represent_failures_and_successes_simultaneously() {
    AtomicBoolean success = new AtomicBoolean();
    ArrayList<Collection> loadCalls = new ArrayList<>();
    DataLoader<Integer, Integer> evenLoader = idLoaderWithErrors(new DataLoaderOptions(), loadCalls);

    Future<Integer> future1 = evenLoader.load(1);
    Future<Integer> future2 = evenLoader.load(2);
    Future<Integer> future3 = evenLoader.load(3);
    Future<Integer> future4 = evenLoader.load(4);
    CompositeFuture result = evenLoader.dispatch();
    result.setHandler(rh -> success.set(true));

    await().untilAtomic(success, is(true));
    assertThat(future1.failed(), is(true));
    assertThat(future1.cause(), instanceOf(IllegalStateException.class));
    assertThat(future2.result(), equalTo(2));
    assertThat(future3.failed(), is(true));
    assertThat(future4.result(), equalTo(4));

    assertThat(loadCalls, equalTo(Collections.singletonList(asList(1, 2, 3, 4))));
}
 
Example 4
Source File: DataLoaderTest.java    From vertx-dataloader with Apache License 2.0 5 votes vote down vote up
@Test
public void should_Resolve_to_empty_list_when_no_keys_supplied() {
    AtomicBoolean success = new AtomicBoolean();
    CompositeFuture futureEmpty = identityLoader.loadMany(Collections.emptyList());
    futureEmpty.setHandler(rh -> {
        assertThat(rh.result().size(), is(0));
        success.set(rh.succeeded());
    });
    identityLoader.dispatch();
    await().untilAtomic(success, is(true));
    assertThat(futureEmpty.list(), empty());
}