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();
}
}
}
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();
}
}
}
}
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));
}
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);
}
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];
}
Aggregations