Search in sources :

Example 1 with BiAction

use of ratpack.func.BiAction in project ratpack by ratpack.

the class DefaultParallelBatch method forEach.

@Override
public Operation forEach(BiAction<? super Integer, ? super T> consumer) {
    AtomicReference<Throwable> error = new AtomicReference<>();
    AtomicBoolean done = new AtomicBoolean();
    AtomicInteger wip = new AtomicInteger();
    return Promise.async(d -> {
        int i = 0;
        Iterator<? extends Promise<T>> iterator = promises.iterator();
        while (iterator.hasNext()) {
            Promise<T> promise = iterator.next();
            final int finalI = i++;
            wip.incrementAndGet();
            if (!iterator.hasNext()) {
                done.set(true);
            }
            Execution.fork().onStart(execInit).onComplete(e -> {
                if (wip.decrementAndGet() == 0 && done.get()) {
                    Throwable t = error.get();
                    if (t == null) {
                        d.success(null);
                    } else {
                        d.error(t);
                    }
                }
            }).start(e -> {
                // noinspection ThrowableResultOfMethodCallIgnored
                if (error.get() == null) {
                    promise.result(t -> {
                        if (t.isError()) {
                            Throwable thisError = t.getThrowable();
                            if (!error.compareAndSet(null, thisError)) {
                                // noinspection ThrowableResultOfMethodCallIgnored
                                Throwable firstError = error.get();
                                if (firstError != thisError) {
                                    firstError.addSuppressed(thisError);
                                }
                            }
                        } else {
                            consumer.execute(finalI, t.getValue());
                        }
                    });
                }
            });
        }
        if (i == 0) {
            d.success(null);
        }
    }).operation();
}
Also used : Types(ratpack.util.Types) Iterator(java.util.Iterator) Execution(ratpack.exec.Execution) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Operation(ratpack.exec.Operation) Promise(ratpack.exec.Promise) AtomicReference(java.util.concurrent.atomic.AtomicReference) BiAction(ratpack.func.BiAction) ParallelBatch(ratpack.exec.util.ParallelBatch) BufferingPublisher(ratpack.stream.internal.BufferingPublisher) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Lists(com.google.common.collect.Lists) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Action(ratpack.func.Action) Subscription(org.reactivestreams.Subscription) TransformablePublisher(ratpack.stream.TransformablePublisher) Collections(java.util.Collections) ExecResult(ratpack.exec.ExecResult) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Promise(ratpack.exec.Promise) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Iterator(java.util.Iterator) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Aggregations

Lists (com.google.common.collect.Lists)1 Collections (java.util.Collections)1 Iterator (java.util.Iterator)1 List (java.util.List)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Subscription (org.reactivestreams.Subscription)1 ExecResult (ratpack.exec.ExecResult)1 Execution (ratpack.exec.Execution)1 Operation (ratpack.exec.Operation)1 Promise (ratpack.exec.Promise)1 ParallelBatch (ratpack.exec.util.ParallelBatch)1 Action (ratpack.func.Action)1 BiAction (ratpack.func.BiAction)1 TransformablePublisher (ratpack.stream.TransformablePublisher)1 BufferingPublisher (ratpack.stream.internal.BufferingPublisher)1 Types (ratpack.util.Types)1