static <T> ReactiveSeq<T> schedule(final Stream<T> stream,final String cron,final ScheduledExecutorService exec) { ReactiveSubscriber<T> sub = reactiveSubscriber(); AtomicBoolean isOpen = new AtomicBoolean(true); Subscription[] s= {null}; sub.onSubscribe(new Subscription() { @Override public void request(long n) { s[0].request(n); } @Override public void cancel() { isOpen.set(false); } }); s[0] = ReactiveSeq.fromStream(stream) .takeWhile(e -> isOpen.get()) .schedule(cron, exec) .connect() .forEach(0, e -> sub.onNext(e),t->sub.onError(t),()->sub.onComplete()); return sub.reactiveStream(); }
/** @Test @Ignore public void limitPushTest(){ ReactiveSubscriber<String> pushable = ReactiveSeq.pushable(); ReactiveSeq<String> stream = pushable.stream(); ReactiveSeq<List<String>> res = stream.map(i->i+"-hello").limit(2) .collectSeq(CyclopsCollectors.toList()); pushable.onNext("hello1"); pushable.onNext("hello2"); pushable.onNext("hello3"); //LimitSpliterator only supports iteration assertThat(res.single().size(),equalTo(3)); } **/ @Test public void forEachWithErrorPush(){ ReactiveSubscriber<String> pushable = Spouts.reactiveSubscriber(); ReactiveSeq<String> stream = pushable.reactiveStream(); // pushable.onComplete(); stream.map(i->i+"-hello").limit(2) .forEach(System.out::println, s->System.out.println("Error" + s)); pushable.onNext("hello1"); pushable.onError(new RuntimeException()); } @Test @Ignore
static <T> ReactiveSeq<T> schedule(final Stream<T> stream,final String cron,final ScheduledExecutorService exec) { ReactiveSubscriber<T> sub = reactiveSubscriber(); AtomicBoolean isOpen = new AtomicBoolean(true); Subscription[] s= {null}; sub.onSubscribe(new Subscription() { @Override public void request(long n) { s[0].request(n); } @Override public void cancel() { isOpen.set(false); } }); s[0] = ReactiveSeq.fromStream(stream) .takeWhile(e -> isOpen.get()) .schedule(cron, exec) .connect() .forEach(0, e -> sub.onNext(e),t->sub.onError(t),()->sub.onComplete()); return sub.reactiveStream(); }