@Override public Topic<T> broadcast() { if (async == Type.NO_BACKPRESSURE) { Queue<T> queue = QueueFactories.<T>boundedNonBlockingQueue(1000) .build() .withTimeout(1); Topic<T> topic = new Topic<>(queue, QueueFactories.<T>boundedNonBlockingQueue(1000)); AtomicBoolean wip = new AtomicBoolean(false); return topic; Queue<T> queue = QueueFactories.<T>boundedNonBlockingQueue(1000) .build() .withTimeout(1); Topic<T> topic = new Topic<>(queue, QueueFactories.<T>boundedNonBlockingQueue(1000)); AtomicBoolean wip = new AtomicBoolean(false); Subscription s = source.subscribe(topic::offer, e -> topic.close(), () -> topic.close());
/** * Construct a Topic using the Queue provided * @param q Queue to back this Topic with */ public Topic(final Queue<T> q) { factory = QueueFactories.unboundedQueue(); distributor.addQueue(q); } public Topic(final Queue<T> q,QueueFactory<T> factory) {
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; }
<T> Queue<T> createQueue() { Queue q; if (!backPressureOn) q = QueueFactories.unboundedNonBlockingQueue() .build(); else q = QueueFactories.boundedQueue(backPressureAfter) .build(); return q; }
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){
@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
/** * Construct a new Topic */ public Topic() { final Queue<T> q = new Queue<T>(); factory = QueueFactories.unboundedQueue(); distributor.addQueue(q); }
@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); }
@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 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")); }
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(); }
@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))); }
/** * Construct a Topic using the Queue provided * @param q Queue to back this Topic with */ public Topic(final Queue<T> q) { factory = QueueFactories.unboundedQueue(); distributor.addQueue(q); } public Topic(final Queue<T> q,QueueFactory<T> factory) {
@Override public ReactiveSeq<T> changes() { if (async == Type.NO_BACKPRESSURE) { Queue<T> discrete = QueueFactories.<T>unboundedNonBlockingQueue() .build() .withTimeout(1); Queue<T> queue = QueueFactories.<T>unboundedNonBlockingQueue() .build(); Signal<T> signal = new Signal<T>(null, queue);
@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(); } }
public SimpleReactStreamImpl(final SimpleReact simpleReact, final Stream<CompletableFuture<U>> stream) { this.simpleReact = simpleReact; final Stream s = stream; this.errorHandler = Optional.empty(); this.lastActive = new EagerStreamWrapper( s, this.errorHandler); this.queueFactory = QueueFactories.unboundedQueue(); this.subscription = new AlwaysContinue(); }
@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() { 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(); } }
/** * Construct a new Topic */ public Topic() { final Queue<T> q = new Queue<T>(); factory = QueueFactories.unboundedQueue(); distributor.addQueue(q); }
default <R> ReactiveSeq<R> parallel(Function<? super Stream<T>,? extends Stream<? extends R>> fn){ Queue<R> queue = QueueFactories.<R>unboundedNonBlockingQueue() .build();