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