Search in sources :

Example 1 with ClosedQueueException

use of com.oath.cyclops.async.adapters.Queue.ClosedQueueException in project cyclops by aol.

the class QueueBasedSubscriber method close.

public void close() {
    counter.completable = true;
    if (queue != null && counter.active.get() == 0) {
        if (counter.closing.compareAndSet(false, true)) {
            counter.closed = true;
            queue.addContinuation(new Continuation(() -> {
                throw new ClosedQueueException();
            }));
            queue.close();
        }
    }
}
Also used : Continuation(com.oath.cyclops.types.futurestream.Continuation) ClosedQueueException(com.oath.cyclops.async.adapters.Queue.ClosedQueueException)

Example 2 with ClosedQueueException

use of com.oath.cyclops.async.adapters.Queue.ClosedQueueException in project cyclops by aol.

the class QueueBasedSubscriber method onComplete.

/* (non-Javadoc)
     * @see org.reactivestreams.Subscriber#onComplete()
     */
@Override
public void onComplete() {
    counter.active.decrementAndGet();
    counter.subscription.remove(subscription);
    if (queue != null && counter.active.get() == 0) {
        if (counter.completable) {
            if (counter.closing.compareAndSet(false, true)) {
                counter.closed = true;
                queue.addContinuation(new Continuation(() -> {
                    final List current = new ArrayList();
                    while (queue.size() > 0) {
                        try {
                            current.add(queue.get());
                        } catch (ClosedQueueException e) {
                            break;
                        }
                    }
                    throw new ClosedQueueException(current);
                }));
                queue.close();
            }
        }
    }
}
Also used : Continuation(com.oath.cyclops.types.futurestream.Continuation) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ClosedQueueException(com.oath.cyclops.async.adapters.Queue.ClosedQueueException)

Example 3 with ClosedQueueException

use of com.oath.cyclops.async.adapters.Queue.ClosedQueueException in project cyclops by aol.

the class FutureStream method chunkSinceLastRead.

/**
 * @return a Stream that batches all completed elements from this stream since last read recover into a collection
 */
default FutureStream<Collection<U>> chunkSinceLastRead() {
    final Queue queue = this.withQueueFactory(QueueFactories.unboundedQueue()).toQueue();
    final Queue.QueueReader reader = new Queue.QueueReader(queue, null);
    class Chunker implements Iterator<Collection<U>> {

        @Override
        public boolean hasNext() {
            return reader.isOpen();
        }

        @Override
        public Collection<U> next() {
            return reader.drainToOrBlock();
        }
    }
    final Chunker chunker = new Chunker();
    final Function<Supplier<U>, Supplier<Collection<U>>> fn = s -> {
        return () -> {
            try {
                return chunker.next();
            } catch (final ClosedQueueException e) {
                throw new ClosedQueueException();
            }
        };
    };
    return fromStream(queue.streamBatchNoTimeout(getSubscription(), fn));
}
Also used : ListX(cyclops.reactive.collections.mutable.ListX) FutureOpterationsImpl(com.oath.cyclops.internal.stream.FutureOpterationsImpl) PersistentCollection(com.oath.cyclops.types.persistent.PersistentCollection) LazyFutureStreamUtils(com.oath.cyclops.internal.react.stream.traits.future.operators.LazyFutureStreamUtils) Future(cyclops.control.Future) Seq(cyclops.data.Seq) SimpleReactFailedStageException(com.oath.cyclops.react.SimpleReactFailedStageException) FastFuture(com.oath.cyclops.internal.react.async.future.FastFuture) ClosedQueueException(com.oath.cyclops.async.adapters.Queue.ClosedQueueException) OperationsOnFuturesImpl(com.oath.cyclops.internal.react.stream.traits.future.operators.OperationsOnFuturesImpl) Monoid(cyclops.function.Monoid) LockSupport(java.util.concurrent.locks.LockSupport) ReactiveSeq(cyclops.reactive.ReactiveSeq) Queue(com.oath.cyclops.async.adapters.Queue) FutureStreamSynchronousPublisher(com.oath.cyclops.types.reactive.FutureStreamSynchronousPublisher) Function3(cyclops.function.Function3) Function4(cyclops.function.Function4) IterableX(com.oath.cyclops.types.traversable.IterableX) ExceptionSoftener(com.oath.cyclops.util.ExceptionSoftener) QueueFactory(com.oath.cyclops.async.adapters.QueueFactory) java.util.function(java.util.function) Tuple4(cyclops.data.tuple.Tuple4) Tuple3(cyclops.data.tuple.Tuple3) Connectable(com.oath.cyclops.types.stream.Connectable) Tuple2(cyclops.data.tuple.Tuple2) java.util(java.util) QueueFactories(com.oath.cyclops.async.QueueFactories) java.util.stream(java.util.stream) CompletableFuture(java.util.concurrent.CompletableFuture) Vector(cyclops.data.Vector) ReactiveStreamsTerminalFutureOperations(com.oath.cyclops.types.reactive.ReactiveStreamsTerminalFutureOperations) LazyStreamWrapper(com.oath.cyclops.internal.react.stream.LazyStreamWrapper) Tuple(cyclops.data.tuple.Tuple) Lambda(cyclops.function.Lambda) Subscriber(org.reactivestreams.Subscriber) SimpleReactProcessingException(com.oath.cyclops.internal.react.exceptions.SimpleReactProcessingException) cyclops.control(cyclops.control) Continueable(com.oath.cyclops.react.async.subscription.Continueable) LazyResultConsumer(com.oath.cyclops.react.collectors.lazy.LazyResultConsumer) Spouts(cyclops.reactive.Spouts) Streams(cyclops.companion.Streams) Executor(java.util.concurrent.Executor) com.oath.cyclops.types.futurestream(com.oath.cyclops.types.futurestream) Publisher(org.reactivestreams.Publisher) lombok.val(lombok.val) Adapter(com.oath.cyclops.async.adapters.Adapter) TimeUnit(java.util.concurrent.TimeUnit) CloseableIterator(com.oath.cyclops.internal.react.stream.CloseableIterator) ForkJoinPool(java.util.concurrent.ForkJoinPool) Subscription(org.reactivestreams.Subscription) Signal(com.oath.cyclops.async.adapters.Signal) QueueTimeoutException(com.oath.cyclops.async.adapters.Queue.QueueTimeoutException) Streamable(cyclops.companion.Streamable) CloseableIterator(com.oath.cyclops.internal.react.stream.CloseableIterator) Queue(com.oath.cyclops.async.adapters.Queue) ClosedQueueException(com.oath.cyclops.async.adapters.Queue.ClosedQueueException)

Example 4 with ClosedQueueException

use of com.oath.cyclops.async.adapters.Queue.ClosedQueueException in project cyclops by aol.

the class LazyToQueue method addToQueue.

@Override
default void addToQueue(final Queue queue) {
    FutureStream str = thenSync(queue::add).self(s -> {
        if (this.getPopulator().isPoolingActive())
            s.peekSync(v -> {
                throw new CompletedException(v);
            });
    });
    final Continuation continuation = queue.getContinuationStrategy().isBlocking() ? str.blockingContinuation(() -> {
        throw new ClosedQueueException();
    }) : str.runContinuation(() -> {
        throw new ClosedQueueException();
    });
    queue.addContinuation(continuation);
}
Also used : Consumer(java.util.function.Consumer) Queue(com.oath.cyclops.async.adapters.Queue) ClosedQueueException(com.oath.cyclops.async.adapters.Queue.ClosedQueueException) Executor(java.util.concurrent.Executor) LazyReact(cyclops.futurestream.LazyReact) Map(java.util.Map) FutureStream(cyclops.futurestream.FutureStream) CompletedException(com.oath.cyclops.internal.react.async.future.CompletedException) Function(java.util.function.Function) CompletedException(com.oath.cyclops.internal.react.async.future.CompletedException) ClosedQueueException(com.oath.cyclops.async.adapters.Queue.ClosedQueueException) FutureStream(cyclops.futurestream.FutureStream)

Example 5 with ClosedQueueException

use of com.oath.cyclops.async.adapters.Queue.ClosedQueueException 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)

Aggregations

ClosedQueueException (com.oath.cyclops.async.adapters.Queue.ClosedQueueException)5 Continuation (com.oath.cyclops.types.futurestream.Continuation)3 Queue (com.oath.cyclops.async.adapters.Queue)2 FastFuture (com.oath.cyclops.internal.react.async.future.FastFuture)2 SimpleReactProcessingException (com.oath.cyclops.internal.react.exceptions.SimpleReactProcessingException)2 Executor (java.util.concurrent.Executor)2 QueueFactories (com.oath.cyclops.async.QueueFactories)1 Adapter (com.oath.cyclops.async.adapters.Adapter)1 QueueTimeoutException (com.oath.cyclops.async.adapters.Queue.QueueTimeoutException)1 QueueFactory (com.oath.cyclops.async.adapters.QueueFactory)1 Signal (com.oath.cyclops.async.adapters.Signal)1 CompletedException (com.oath.cyclops.internal.react.async.future.CompletedException)1 CloseableIterator (com.oath.cyclops.internal.react.stream.CloseableIterator)1 LazyStreamWrapper (com.oath.cyclops.internal.react.stream.LazyStreamWrapper)1 LazyFutureStreamUtils (com.oath.cyclops.internal.react.stream.traits.future.operators.LazyFutureStreamUtils)1 OperationsOnFuturesImpl (com.oath.cyclops.internal.react.stream.traits.future.operators.OperationsOnFuturesImpl)1 FutureOpterationsImpl (com.oath.cyclops.internal.stream.FutureOpterationsImpl)1 SimpleReactFailedStageException (com.oath.cyclops.react.SimpleReactFailedStageException)1 Continueable (com.oath.cyclops.react.async.subscription.Continueable)1 LazyResultConsumer (com.oath.cyclops.react.collectors.lazy.LazyResultConsumer)1