Search in sources :

Example 1 with Queue

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

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

the class FutureStream method switchOnNextValue.

/**
 * Merges this stream and the supplied Streams into a single Stream where the next value
 * is the next returned across any of the involved Streams. Suitable for merging infinite streams
 *
 * <pre>
 * {@code
 * 	FutureStream<Integer> fast =  ... //  [1,2,3,4,5,6,7..]
 * 	FutureStream<Integer> slow =  ... //  [100,200,300,400,500,600..]
 *
 *  FutureStream<Integer> merged = fast.switchOnNextValue(Stream.of(slow));  //[1,2,3,4,5,6,7,8,100,9,10,11,12,13,14,15,16,200..]
 * }
 * </pre>
 *
 * @param streams
 * @return
 */
default <R> FutureStream<R> switchOnNextValue(final Stream<FutureStream> streams) {
    final Queue queue = Queue.createMergeQueue();
    addToQueue(queue);
    streams.forEach(s -> s.addToQueue(queue));
    return fromStream(queue.stream(this.getSubscription()));
}
Also used : Queue(com.oath.cyclops.async.adapters.Queue)

Example 3 with Queue

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

the class VertxTest method downloadUrls.

@Test
@Ignore
public void downloadUrls() {
    // cyclops2-react async.Queues
    Queue<String> downloadQueue = new Queue<String>();
    Queue<String> completedQueue = new Queue<String>();
    // vert.x meets cyclops2-react
    Vertx vertx = Vertx.factory.vertx();
    LazyReact react = new LazyReact(c -> vertx.runOnContext(v -> c.run()));
    // populate the download queue asynchronously
    ReactiveSeq.of("www.aol.com", "www.rte.ie", "www.aol.com").peek(next -> System.out.println("adding toNested download queue " + next)).runFuture(c -> vertx.runOnContext(v -> c.run()), t -> t.forEach(downloadQueue::add, System.err::println));
    // download asynchronously : all cyclops2-react tasks are passed into vert.x
    react.fromStream(downloadQueue.stream()).peek(System.out::println).map(url -> vertx.createHttpClient().getNow(url, "", resp -> resp.bodyHandler(body -> completedQueue.add(body.getString(0, body.length()))))).run();
    // handle the results
    completedQueue.stream().peek(next -> System.out.println("just downloaded" + next)).forEach(System.out::println);
}
Also used : HttpServerRequest(io.vertx.core.http.HttpServerRequest) QueueFactories(com.oath.cyclops.async.QueueFactories) LazyReact(cyclops.futurestream.LazyReact) HttpServer(io.vertx.core.http.HttpServer) Vertx(io.vertx.core.Vertx) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) Assert.assertThat(org.junit.Assert.assertThat) ReactiveSeq(cyclops.reactive.ReactiveSeq) Tuple.tuple(cyclops.data.tuple.Tuple.tuple) Queue(com.oath.cyclops.async.adapters.Queue) Ignore(org.junit.Ignore) Matchers.equalTo(org.hamcrest.Matchers.equalTo) SimpleReact(cyclops.futurestream.SimpleReact) HttpServerOptions(io.vertx.core.http.HttpServerOptions) WaitStrategy(com.oath.cyclops.async.wait.WaitStrategy) LazyReact(cyclops.futurestream.LazyReact) Vertx(io.vertx.core.Vertx) Queue(com.oath.cyclops.async.adapters.Queue) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with Queue

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

the class VertxTest method httpServer.

@Test
@Ignore
public void httpServer() {
    Vertx vertx = Vertx.factory.vertx();
    CompletableFuture<HttpServer> server = new CompletableFuture<>();
    Queue<HttpServerRequest> reqs = QueueFactories.<HttpServerRequest>boundedNonBlockingQueue(1000, WaitStrategy.spinWait()).build();
    vertx.createHttpServer(new HttpServerOptions().setPort(8080).setHost("localhost")).requestHandler(event -> {
        reqs.add(event);
        System.out.println(event.absoluteURI());
    }).listen(e -> {
        if (e.succeeded())
            server.complete(e.result());
        else
            server.completeExceptionally(e.cause());
    });
    LazyReact react = new LazyReact(c -> vertx.runOnContext(v -> c.run()));
    react.fromStream(reqs.stream()).filter(req -> req.getParam("num") != null).peek(i -> System.out.println("grouping " + i)).grouped(2).map(list -> tuple(list.getOrElse(0, null).response(), list.getOrElse(1, null).response(), getParam(list.getOrElse(0, null)), getParam(list.getOrElse(1, null)))).peek(i -> System.out.println("peeking + " + i)).peek(t -> t._1().end("adding " + (t._3() + t._4()))).peek(t -> t._2().end("multiplying " + t._3() * t._4())).run();
    new SimpleReact(c -> vertx.runOnContext(v -> c.run())).from(server).then(s -> "server started").onFail(e -> "failed toNested skip " + e.getMessage()).peek(System.out::println);
    while (true) {
    }
}
Also used : HttpServerRequest(io.vertx.core.http.HttpServerRequest) QueueFactories(com.oath.cyclops.async.QueueFactories) LazyReact(cyclops.futurestream.LazyReact) HttpServer(io.vertx.core.http.HttpServer) Vertx(io.vertx.core.Vertx) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test) Assert.assertThat(org.junit.Assert.assertThat) ReactiveSeq(cyclops.reactive.ReactiveSeq) Tuple.tuple(cyclops.data.tuple.Tuple.tuple) Queue(com.oath.cyclops.async.adapters.Queue) Ignore(org.junit.Ignore) Matchers.equalTo(org.hamcrest.Matchers.equalTo) SimpleReact(cyclops.futurestream.SimpleReact) HttpServerOptions(io.vertx.core.http.HttpServerOptions) WaitStrategy(com.oath.cyclops.async.wait.WaitStrategy) CompletableFuture(java.util.concurrent.CompletableFuture) LazyReact(cyclops.futurestream.LazyReact) SimpleReact(cyclops.futurestream.SimpleReact) HttpServerRequest(io.vertx.core.http.HttpServerRequest) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Vertx(io.vertx.core.Vertx) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with Queue

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

the class PipesTest method oneValueOrError.

@Test
public void oneValueOrError() throws InterruptedException {
    Queue q = new Queue<>();
    pipes.register("hello", q);
    pipes.push("hello", "world");
    pipes.push("hello", "world2");
    q.close();
    assertThat(pipes.oneValueOrError("hello").toOptional().get().get(), equalTo(Option.some("world")));
    assertThat(pipes.oneValueOrError("hello").toOptional().get().get(), equalTo(Option.some("world2")));
}
Also used : Queue(com.oath.cyclops.async.adapters.Queue) Test(org.junit.Test)

Aggregations

Queue (com.oath.cyclops.async.adapters.Queue)44 Test (org.junit.Test)33 LazyReact (cyclops.futurestream.LazyReact)22 List (java.util.List)18 QueueFactories (com.oath.cyclops.async.QueueFactories)17 Tuple2 (cyclops.data.tuple.Tuple2)17 Iterator (java.util.Iterator)17 Collectors (java.util.stream.Collectors)17 ReactiveSeq (cyclops.reactive.ReactiveSeq)16 Ignore (org.junit.Ignore)16 FutureStream (cyclops.futurestream.FutureStream)15 Arrays.asList (java.util.Arrays.asList)15 Matchers.greaterThan (org.hamcrest.Matchers.greaterThan)15 Matchers.is (org.hamcrest.Matchers.is)15 ThreadPools (com.oath.cyclops.react.ThreadPools)14 Tuple.tuple (cyclops.data.tuple.Tuple.tuple)14 Matchers.equalTo (org.hamcrest.Matchers.equalTo)14 Signal (com.oath.cyclops.async.adapters.Signal)13 ForkJoinPool (java.util.concurrent.ForkJoinPool)13 Stream (java.util.stream.Stream)13