Search in sources :

Example 1 with Operation

use of ratpack.exec.Operation 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)

Example 2 with Operation

use of ratpack.exec.Operation in project ratpack by ratpack.

the class DefaultSession method save.

@Override
public Operation save() {
    return Operation.of(() -> {
        if (state != State.NOT_LOADED) {
            ByteBuf serialized = serialize();
            storeAdapter.store(sessionId.getValue(), serialized).wiretap(o -> serialized.release()).then(() -> state = State.CLEAN);
        }
    });
}
Also used : Iterables(com.google.common.collect.Iterables) java.util(java.util) Logger(org.slf4j.Logger) ratpack.session(ratpack.session) Types(ratpack.util.Types) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) LoggerFactory(org.slf4j.LoggerFactory) Operation(ratpack.exec.Operation) Promise(ratpack.exec.Promise) ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) Response(ratpack.http.Response) Ordering(com.google.common.collect.Ordering) ByteBuf(io.netty.buffer.ByteBuf) java.io(java.io) FluentIterable(com.google.common.collect.FluentIterable) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

Operation (ratpack.exec.Operation)2 Promise (ratpack.exec.Promise)2 Types (ratpack.util.Types)2 FluentIterable (com.google.common.collect.FluentIterable)1 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1 Iterables (com.google.common.collect.Iterables)1 Lists (com.google.common.collect.Lists)1 Ordering (com.google.common.collect.Ordering)1 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)1 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)1 ByteBufOutputStream (io.netty.buffer.ByteBufOutputStream)1 java.io (java.io)1 java.util (java.util)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