Search in sources :

Example 1 with SimpleReactProcessingException

use of com.oath.cyclops.internal.react.exceptions.SimpleReactProcessingException in project cyclops by aol.

the class Runner method runContinuations.

public Continuation runContinuations(final LazyStreamWrapper lastActive, final EmptyCollector collector, boolean blocking) {
    final Iterator<FastFuture> it = lastActive.injectFutures().iterator();
    final Continuation[] cont = new Continuation[1];
    final Continuation finish = new Continuation(() -> {
        collector.afterResults(() -> {
            runnable.run();
            throw new ClosedQueueException();
        });
        return Continuation.empty();
    });
    final Continuation blockingFinish = new Continuation(() -> {
        collector.getResults();
        runnable.run();
        throw new ClosedQueueException();
    });
    final Continuation finishNoCollect = new Continuation(() -> {
        runnable.run();
        throw new ClosedQueueException();
    });
    cont[0] = new Continuation(() -> {
        try {
            if (it.hasNext()) {
                final FastFuture f = it.next();
                // if FastFuture has been filtered out, we need to move to the next one instead
                handleFilter(cont, f);
                collector.accept(f);
            }
            if (it.hasNext())
                return cont[0];
            else {
                return blocking ? blockingFinish.proceed() : finish.proceed();
            }
        } catch (final SimpleReactProcessingException e) {
        } catch (final java.util.concurrent.CompletionException e) {
        } catch (final Throwable e) {
            collector.getSafeJoin().apply(FastFuture.failedFuture(e));
        }
        return finishNoCollect;
    });
    return cont[0];
}
Also used : Continuation(com.oath.cyclops.types.futurestream.Continuation) FastFuture(com.oath.cyclops.internal.react.async.future.FastFuture) SimpleReactProcessingException(com.oath.cyclops.internal.react.exceptions.SimpleReactProcessingException) ClosedQueueException(com.oath.cyclops.async.adapters.Queue.ClosedQueueException)

Example 2 with SimpleReactProcessingException

use of com.oath.cyclops.internal.react.exceptions.SimpleReactProcessingException in project cyclops by aol.

the class FutureStreamSynchronousPublisher method subscribe.

/* (non-Javadoc)
     * @see org.reactivestreams.Publisher#forEachAsync(org.reactivestreams.Subscriber)
     */
@Override
default void subscribe(final Subscriber<? super T> s) {
    try {
        forwardErrors(t -> s.onError(t));
        final Queue<T> queue = toQueue();
        final Iterator<CompletableFuture<T>> it = queue.streamCompletableFutures().iterator();
        final Subscription sub = new Subscription() {

            volatile boolean complete = false;

            volatile boolean cancelled = false;

            final LinkedList<Long> requests = new LinkedList<Long>();

            boolean active = false;

            private void handleNext(final T data) {
                if (!cancelled) {
                    s.onNext(data);
                }
            }

            @Override
            public void request(final long n) {
                if (n < 1) {
                    s.onError(new IllegalArgumentException("3.9 While the Subscription is not cancelled, Subscription.request(long n) MUST throw a java.lang.IllegalArgumentException if the argument is <= 0."));
                }
                requests.add(n);
                final List<CompletableFuture> results = new ArrayList<>();
                if (active) {
                    return;
                }
                active = true;
                try {
                    while (!cancelled && requests.size() > 0) {
                        final long n2 = requests.peek();
                        for (int i = 0; i < n2; i++) {
                            try {
                                if (it.hasNext()) {
                                    handleNext(s, it, results);
                                } else {
                                    handleComplete(results, s);
                                    break;
                                }
                            } catch (final Throwable t) {
                                s.onError(t);
                            }
                        }
                        requests.pop();
                    }
                } finally {
                    active = false;
                }
            }

            private void handleComplete(final List<CompletableFuture> results, final Subscriber<? super T> s) {
                if (!complete && !cancelled) {
                    complete = true;
                    if (results.size() > 0) {
                        CompletableFuture.allOf(results.stream().map(cf -> cf.exceptionally(e -> null)).collect(Collectors.toList()).toArray(new CompletableFuture[results.size()])).thenAccept(a -> callOnComplete(s)).exceptionally(e -> {
                            callOnComplete(s);
                            return null;
                        });
                    } else {
                        callOnComplete(s);
                    }
                }
            }

            private void callOnComplete(final Subscriber<? super T> s) {
                s.onComplete();
            }

            private void handleNext(final Subscriber<? super T> s, final Iterator<CompletableFuture<T>> it, final List<CompletableFuture> results) {
                results.add(it.next().thenAccept(r -> {
                    s.onNext(r);
                }).exceptionally(t -> {
                    s.onError(t);
                    return null;
                }));
                final List<CompletableFuture> newResults = results.stream().filter(cf -> cf.isDone()).collect(Collectors.toList());
                results.removeAll(newResults);
            }

            @Override
            public void cancel() {
                cancelled = true;
                forwardErrors(t -> {
                });
                queue.closeAndClear();
            }
        };
        s.onSubscribe(sub);
    } catch (final SimpleReactProcessingException e) {
    }
}
Also used : SimpleReactProcessingException(com.oath.cyclops.internal.react.exceptions.SimpleReactProcessingException) Iterator(java.util.Iterator) Publisher(org.reactivestreams.Publisher) CompletableFuture(java.util.concurrent.CompletableFuture) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) List(java.util.List) Queue(com.oath.cyclops.async.adapters.Queue) LazyStreamWrapper(com.oath.cyclops.internal.react.stream.LazyStreamWrapper) Subscription(org.reactivestreams.Subscription) LinkedList(java.util.LinkedList) Subscriber(org.reactivestreams.Subscriber) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) CompletableFuture(java.util.concurrent.CompletableFuture) Subscriber(org.reactivestreams.Subscriber) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Subscription(org.reactivestreams.Subscription) SimpleReactProcessingException(com.oath.cyclops.internal.react.exceptions.SimpleReactProcessingException)

Aggregations

SimpleReactProcessingException (com.oath.cyclops.internal.react.exceptions.SimpleReactProcessingException)2 Queue (com.oath.cyclops.async.adapters.Queue)1 ClosedQueueException (com.oath.cyclops.async.adapters.Queue.ClosedQueueException)1 FastFuture (com.oath.cyclops.internal.react.async.future.FastFuture)1 LazyStreamWrapper (com.oath.cyclops.internal.react.stream.LazyStreamWrapper)1 Continuation (com.oath.cyclops.types.futurestream.Continuation)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Consumer (java.util.function.Consumer)1 Collectors (java.util.stream.Collectors)1 Publisher (org.reactivestreams.Publisher)1 Subscriber (org.reactivestreams.Subscriber)1 Subscription (org.reactivestreams.Subscription)1