/** * Construct an async.Queue backed by a JDK Queue from the provided QueueFactory * * @param factory QueueFactory to extract JDK Queue from */ public Queue(final QueueFactory<T> factory) { final Queue<T> q = factory.build(); this.queue = q.queue; timeout = q.timeout; timeUnit = q.timeUnit; maxPoisonPills = q.maxPoisonPills; offerTimeout = q.offerTimeout; offerTimeUnit = q.offerTimeUnit; this.consumerWait = q.consumerWait; this.producerWait = q.producerWait; }
default <R> R foldParallel(Function<? super Stream<T>,? extends R> fn){ Queue<T> queue = QueueFactories.<T>unboundedNonBlockingQueue().build().withTimeout(1); AtomicReference<Continuation> ref = new AtomicReference<>(null); Continuation cont = new Continuation(()->{ if(ref.get()==null && ref.compareAndSet(null,Continuation.empty())){ try { //use the first consuming thread to tell this Stream onto the Queue this.spliterator().forEachRemaining(queue::offer); }finally { queue.close(); } } return Continuation.empty(); }); ; queue.addContinuation(cont); return fn.apply(queue.jdkStream().parallel()); } default <R> R foldParallel(ForkJoinPool fj,Function<? super Stream<T>,? extends R> fn){
private Queue<T> getNextQueue() { if (index >= this.distributor.getSubscribers() .size()) { this.distributor.addQueue(factory.build()); } return this.distributor.getSubscribers() .getOrElse(index++,null); }
@Override public Stream<T> unwrapStream() { if (async == Type.NO_BACKPRESSURE) { Queue<T> queue = QueueFactories.<T>unboundedNonBlockingQueue() .build(); AtomicBoolean wip = new AtomicBoolean(false); Continuation cont = new Continuation(() -> { if (wip.compareAndSet(false, true)) { this.source.subscribeAll(queue::offer, i -> { queue.close(); }, () -> queue.close()); } return Continuation.empty(); }); queue.addContinuation(cont); return queue.stream(); } return StreamSupport.stream(new OperatorToIterable<>(source, this.defaultErrorHandler, async == BACKPRESSURE).spliterator(), false); }
public ReactiveSeq<T> changes(){ com.oath.cyclops.async.adapters.Queue<T> queue = QueueFactories.<T>unboundedNonBlockingQueue() .build(); Spliterator<T> copy = copy(); Continuation[] contRef ={null}; Signal<T> signal = new Signal<T>(null, queue); AtomicBoolean wip = new AtomicBoolean(false); Continuation cont = new Continuation(()->{ if(wip.compareAndSet(false,true)) { if(!copy.tryAdvance(signal::set)){ signal.close(); return Continuation.empty(); } wip.set(false); } return contRef[0]; }); contRef[0]= cont; queue.addContinuation(cont); return signal.getDiscrete().stream(); }
default Topic<T> broadcast(){ Queue<T> queue = QueueFactories.<T>unboundedNonBlockingQueue() .build() .withTimeout(1); Topic<T> topic = new Topic<T>(queue,QueueFactories.<T>unboundedNonBlockingQueue()); AtomicBoolean wip = new AtomicBoolean(false); Spliterator<T> split = this.spliterator(); Continuation ref[] = {null}; Continuation cont = new Continuation(()->{ if(wip.compareAndSet(false,true)){ try { //use the first consuming thread to tell this Stream onto the Queue if(!split.tryAdvance(topic::offer)){ topic.close(); return Continuation.empty(); } }finally { wip.set(false); } } return ref[0]; }); ref[0]=cont; queue.addContinuation(cont); return topic; }
if (async == Type.NO_BACKPRESSURE) { Queue<T> discrete = QueueFactories.<T>unboundedNonBlockingQueue() .build() .withTimeout(1); } else { Queue<T> queue = QueueFactories.<T>unboundedNonBlockingQueue() .build(); Signal<T> signal = new Signal<T>(null, queue); Subscription sub = source.subscribe(signal::set, i -> {
@Override public Iterator<T> iterator() { if (async == Type.NO_BACKPRESSURE) { Queue<T> queue = QueueFactories.<T>unboundedNonBlockingQueue() .build(); AtomicBoolean wip = new AtomicBoolean(false); Subscription[] sub = {null}; Continuation cont = new Continuation(() -> { if (wip.compareAndSet(false, true)) { this.source.subscribeAll(queue::offer, i -> queue.close(), () -> queue.close()); } return Continuation.empty(); }); queue.addContinuation(cont); return queue.stream().iterator(); } return new OperatorToIterable<>(source, this.defaultErrorHandler, async == BACKPRESSURE).iterator(); }
@Test public void publishToAndMerge(){ com.oath.cyclops.async.adapters.Queue<Integer> queue = QueueFactories.<Integer>boundedNonBlockingQueue(10) .build(); Thread t= new Thread( ()-> { while(true) { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Closing!"); queue.close(); } }); t.start(); assertThat(Spouts.of(1,2,3) .publishTo(queue) .peek(System.out::println) .merge(queue) .toList(), Matchers.equalTo(Arrays.asList(1,1,2,2,3,3))); } @Test
@Test public void adapter(){ Adapter<Integer> adapter = QueueFactories.<Integer>unboundedQueue() .build(); String result = Eithers.adapter(adapter) .fold(queue->"we have a queue", topic->"we have a topic"); assertThat(result,equalTo("we have a queue")); }
@Test public void publishTest() { for (int k = 0; k < ITERATIONS; k++) { Queue<Integer> queue = QueueFactories.<Integer>boundedNonBlockingQueue(10) .build(); Thread t = new Thread(() -> { try { System.out.println("Sleeping!"); Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Waking!"); System.out.println("Closing! " + queue.size()); queue.close(); }); t.start(); of(1, 2, 3).peek(i -> System.out.println("publishing " + i)) .publishTo(queue) .forEach(System.out::println); assertThat(queue.stream().collect(Collectors.toList()), equalTo(Arrays.asList(1, 2, 3))); t = null; System.gc(); } }
@Test public void publishToAndMerge(){ com.oath.cyclops.async.adapters.Queue<Integer> queue = QueueFactories.<Integer>boundedNonBlockingQueue(10) .build(); Thread t= new Thread( ()-> { while(true) { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Closing!"); queue.close(); } }); t.start(); assertThat(ReactiveSeq.of(1,2,3) .publishTo(queue) .peek(System.out::println) .merge(queue) .toList(),equalTo(Arrays.asList(1,1,2,2,3,3))); }
@Test public void mergeAdapterTest() { for (int k = 0; k < ITERATIONS; k++) { Queue<Integer> queue = QueueFactories.<Integer>boundedNonBlockingQueue(10) .build(); Thread t = new Thread(() -> { queue.add(1); queue.add(2); queue.add(3); try { System.out.println("Sleeping!"); Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Waking!"); System.out.println("Closing! " + queue.size()); queue.close(); }); t.start(); assertThat(this.<Integer>of().peek(i -> System.out.println("publishing " + i)) .merge(queue).collect(Collectors.toList()), equalTo(Arrays.asList(1, 2, 3))); t = null; System.gc(); } }
@SuppressWarnings("unchecked") @Override <T> Queue<T> createQueue() { return (Queue<T>) q.build(); } };
@Test public void publishToAndMerge() { for (int k = 0; k < ITERATIONS; k++) { System.out.println("Publish toNested and zip iteration " + k); com.oath.cyclops.async.adapters.Queue<Integer> queue = QueueFactories.<Integer>boundedNonBlockingQueue(10) .build(); Thread t = new Thread(() -> { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Closing! " + queue.size()); queue.close(); }); t.start(); AtomicBoolean complete = new AtomicBoolean(false); AtomicBoolean start = new AtomicBoolean(false); List<Integer> list = of(1, 2, 3) .publishTo(queue) .peek(System.out::println) .merge(queue) .toList(); assertThat(list, hasItems(1, 2, 3)); assertThat(list.size(), equalTo(6)); System.gc(); } }
default <R> ReactiveSeq<R> parallel(Function<? super Stream<T>,? extends Stream<? extends R>> fn){ Queue<R> queue = QueueFactories.<R>unboundedNonBlockingQueue() .build();
default <R> ReactiveSeq<R> parallel(ForkJoinPool fj,Function<? super Stream<T>,? extends Stream<? extends R>> fn){ Queue<R> queue = QueueFactories.<R>unboundedNonBlockingQueue() .build();
@Test public void mergeAdapterTest1() { for (int k = 0; k < ITERATIONS; k++) { System.out.println("Test iteration " + k); Queue<Integer> queue = QueueFactories.<Integer>boundedNonBlockingQueue(10) .build(); Thread t = new Thread(() -> { queue.add(1); queue.add(2); queue.add(3); try { // System.out.println("Sleeping!"); Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } // System.out.println("Closing! " + queue.size()); queue.close(); }); t.start(); assertThat(this.<Integer>of(10).peek(i -> System.out.println("publishing " + i)) .merge(queue).collect(Collectors.toList()), hasItems(10, 1, 2, 3)); t = null; System.gc(); } }