Search in sources :

Example 1 with Subscription

use of com.oath.cyclops.react.async.subscription.Subscription in project cyclops by aol.

the class LazyReact method iterate.

/**
 * Iterate infinitely using the supplied seed and function
 * Iteration is synchronized to support multiple threads using the same iterator.
 *
 * <pre>
 * {@code
 *  new LazyReact().objectPoolingOn()
 *                       .iterate(1,i->i+1)
 *                       .limit(1_000_000)
 *                       .map(this::process)
 *                       .forEach(this::save);
 *
 * }
 * </pre>
 *
 * @see FutureStream#iterate for an alternative which does not synchronize iteration
 * @param seed Initial value
 * @param f Function that performs the iteration
 * @return FutureStream
 */
public <U> FutureStream<U> iterate(final U seed, final UnaryOperator<U> f) {
    final Subscription sub = new Subscription();
    final Supplier<U> supplier = new Supplier<U>() {

        @SuppressWarnings("unchecked")
        U t = (U) NONE;

        @Override
        public U get() {
            return t = t == NONE ? seed : f.apply(t);
        }
    };
    return construct(StreamSupport.<U>stream(new InfiniteClosingSpliteratorFromSupplier<U>(Long.MAX_VALUE, supplier, sub), false));
}
Also used : InfiniteClosingSpliteratorFromSupplier(com.oath.cyclops.internal.react.stream.InfiniteClosingSpliteratorFromSupplier) Supplier(java.util.function.Supplier) InfiniteClosingSpliteratorFromSupplier(com.oath.cyclops.internal.react.stream.InfiniteClosingSpliteratorFromSupplier) Subscription(com.oath.cyclops.react.async.subscription.Subscription)

Example 2 with Subscription

use of com.oath.cyclops.react.async.subscription.Subscription in project cyclops by aol.

the class BatchingInvestigationsTest method streamBatch.

@Test
public void streamBatch() {
    Queue<String> queue = QueueFactories.<String>unboundedQueue().build();
    new Thread(() -> {
        for (int i = 0; i < 10; i++) {
            queue.offer("New message " + i);
            sleep(10000);
        }
        queue.close();
    }).start();
    long toRun = TimeUnit.MILLISECONDS.toNanos(500l);
    queue.streamBatch(new Subscription(), source -> {
        return () -> {
            List<String> result = new ArrayList<>();
            long start = System.nanoTime();
            while (result.size() < 10 && (System.nanoTime() - start) < toRun) {
                try {
                    String next = source.apply(1l, TimeUnit.MILLISECONDS);
                    if (next != null) {
                        result.add(next);
                    }
                } catch (Queue.QueueTimeoutException e) {
                }
            }
            if (result.size() > 0) {
                System.out.println("Result " + result);
            }
            start = System.nanoTime();
            return result;
        };
    }).filter(l -> l.size() > 0).to(s -> new LazyReact(ThreadPools.getSequential()).fromStream(s)).async().peek(System.out::println).run();
    while (true) {
    }
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Queue(com.oath.cyclops.async.adapters.Queue) QueueFactories(com.oath.cyclops.async.QueueFactories) Ignore(org.junit.Ignore) LazyReact(cyclops.futurestream.LazyReact) ThreadPools(com.oath.cyclops.react.ThreadPools) Test(org.junit.Test) StreamSource(cyclops.stream.StreamSource) Subscription(com.oath.cyclops.react.async.subscription.Subscription) ArrayList(java.util.ArrayList) LazyReact(cyclops.futurestream.LazyReact) ArrayList(java.util.ArrayList) Subscription(com.oath.cyclops.react.async.subscription.Subscription) Queue(com.oath.cyclops.async.adapters.Queue) Test(org.junit.Test)

Example 3 with Subscription

use of com.oath.cyclops.react.async.subscription.Subscription in project cyclops by aol.

the class Queue method streamGroupedBySizeAndTime.

public ReactiveSeq<Seq<T>> streamGroupedBySizeAndTime(int size, long time, TimeUnit t) {
    long toRun = t.toNanos(time);
    return streamBatch(new Subscription(), source -> {
        return () -> {
            List<T> result = new ArrayList<>();
            long start = System.nanoTime();
            try {
                while (result.size() < size && checkTime(System.nanoTime(), start, toRun)) {
                    try {
                        T next = source.apply(100l, TimeUnit.MICROSECONDS);
                        if (next != null) {
                            result.add(next);
                        }
                    } catch (Queue.QueueTimeoutException e) {
                    }
                }
            } catch (Queue.ClosedQueueException e) {
                if (result.size() > 0) {
                    List list = new ArrayList<>();
                    list.add(result);
                    throw new ClosedQueueException(list);
                }
            }
            return result;
        };
    }).filter(l -> l.size() > 0).map(Seq::fromIterable);
}
Also used : java.util(java.util) Setter(lombok.Setter) Continuation(com.oath.cyclops.types.futurestream.Continuation) QueueFactories(com.oath.cyclops.async.QueueFactories) Getter(lombok.Getter) BiFunction(java.util.function.BiFunction) Wither(lombok.experimental.Wither) CompletableFuture(java.util.concurrent.CompletableFuture) Subscription(com.oath.cyclops.react.async.subscription.Subscription) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Seq(cyclops.data.Seq) AccessLevel(lombok.AccessLevel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StreamSupport(java.util.stream.StreamSupport) AlwaysContinue(com.oath.cyclops.react.async.subscription.AlwaysContinue) WaitStrategy(com.oath.cyclops.async.wait.WaitStrategy) SimpleReactProcessingException(com.oath.cyclops.internal.react.exceptions.SimpleReactProcessingException) DirectWaitStrategy(com.oath.cyclops.async.wait.DirectWaitStrategy) Continueable(com.oath.cyclops.react.async.subscription.Continueable) BlockingQueue(java.util.concurrent.BlockingQueue) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) SimpleTimer(com.oath.cyclops.util.SimpleTimer) TimeUnit(java.util.concurrent.TimeUnit) ReactiveSeq(cyclops.reactive.ReactiveSeq) Stream(java.util.stream.Stream) ExceptionSoftener(com.oath.cyclops.util.ExceptionSoftener) AllArgsConstructor(lombok.AllArgsConstructor) Subscription(com.oath.cyclops.react.async.subscription.Subscription) Seq(cyclops.data.Seq) ReactiveSeq(cyclops.reactive.ReactiveSeq)

Aggregations

Subscription (com.oath.cyclops.react.async.subscription.Subscription)3 QueueFactories (com.oath.cyclops.async.QueueFactories)2 TimeUnit (java.util.concurrent.TimeUnit)2 Supplier (java.util.function.Supplier)2 Queue (com.oath.cyclops.async.adapters.Queue)1 DirectWaitStrategy (com.oath.cyclops.async.wait.DirectWaitStrategy)1 WaitStrategy (com.oath.cyclops.async.wait.WaitStrategy)1 SimpleReactProcessingException (com.oath.cyclops.internal.react.exceptions.SimpleReactProcessingException)1 InfiniteClosingSpliteratorFromSupplier (com.oath.cyclops.internal.react.stream.InfiniteClosingSpliteratorFromSupplier)1 ThreadPools (com.oath.cyclops.react.ThreadPools)1 AlwaysContinue (com.oath.cyclops.react.async.subscription.AlwaysContinue)1 Continueable (com.oath.cyclops.react.async.subscription.Continueable)1 Continuation (com.oath.cyclops.types.futurestream.Continuation)1 ExceptionSoftener (com.oath.cyclops.util.ExceptionSoftener)1 SimpleTimer (com.oath.cyclops.util.SimpleTimer)1 Seq (cyclops.data.Seq)1 LazyReact (cyclops.futurestream.LazyReact)1 ReactiveSeq (cyclops.reactive.ReactiveSeq)1 StreamSource (cyclops.stream.StreamSource)1 java.util (java.util)1