Search in sources :

Example 1 with BufferingPublisher

use of ratpack.stream.internal.BufferingPublisher in project ratpack by ratpack.

the class DefaultParallelBatch method publisher.

@Override
public TransformablePublisher<T> publisher() {
    Iterator<? extends Promise<T>> iterator = promises.iterator();
    return new BufferingPublisher<>(Action.noop(), write -> {
        return new Subscription() {

            volatile boolean cancelled;

            volatile boolean complete;

            final AtomicLong finished = new AtomicLong();

            volatile long started;

            @Override
            public void request(long n) {
                while (n-- > 0 && !cancelled) {
                    if (iterator.hasNext()) {
                        ++started;
                        Promise<T> promise = iterator.next();
                        if (!iterator.hasNext()) {
                            complete = true;
                        }
                        Execution.fork().onStart(execInit).onComplete(e -> {
                            long finished = this.finished.incrementAndGet();
                            if (finished == started && complete && !cancelled) {
                                write.complete();
                            }
                        }).start(e -> promise.onError(write::error).then(write::item));
                    } else {
                        return;
                    }
                }
            }

            @Override
            public void cancel() {
                cancelled = true;
            }
        };
    });
}
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) ExecResult(ratpack.exec.ExecResult) BufferingPublisher(ratpack.stream.internal.BufferingPublisher) AtomicLong(java.util.concurrent.atomic.AtomicLong) Subscription(org.reactivestreams.Subscription)

Aggregations

Lists (com.google.common.collect.Lists)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