简体   繁体   English

RxJava 2也许不产生输出

[英]RxJava 2 Maybe does not produce output

I have a single consumer and multiple producers - the records need to be batched before the consumer will attempt to execute some expensive operation. 我有一个消费者和多个生产者-记录需要先进行批处理,然后消费者才能尝试执行一些昂贵的操作。

Here is my attempt to implement the above using RxJava window and reduce functions: 这是我尝试使用RxJava窗口并简化功能来实现上述功能:

import io.reactivex.Maybe;
import io.reactivex.Observable;
import io.reactivex.subjects.PublishSubject;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

public class Rxtest {
  private PublishSubject<String> bus = PublishSubject.create();
  private Maybe<Observable<String>> maybe;

  public Rxtest() {
    maybe = bus
        .window(10, TimeUnit.SECONDS, 10)
        .reduce( (fObs, sObs) -> fObs.zipWith(sObs, (f, s) -> f + s));

    maybe.subscribe(obs -> obs.subscribe(System.out::println, Throwable::printStackTrace,
        () -> System.out.println("Done here")));
  }

  public static void main(String[] args) throws InterruptedException {
    Rxtest test = new Rxtest();
    Stream.iterate(0, i -> i+1)
        .forEach(i -> {
          test.bus.onNext(String.valueOf(i));
          return;
        });
    Thread.sleep(100000L);
  }
}

The output I expected: 我期望的输出:

12345678910
11121314151617181920

....
....
Done here

Instead, I get nothing. 相反,我什么也没得到。

The following will batch up 10 seconds worth of requests and process them. 以下内容将分批处理价值10秒的请求。

observable
  .buffer( 10, TimeUnit.SECONDS )
  .subscribe( listOfString -> process( listOfString ) );

This will batch up 10 seconds or 100 items and process them: 这将分批处理10秒或100个项目:

observable
  .buffer( 10, TimeUnit.SECONDS, 100 )
  .subscribe( listOfString -> process( listOfString ) );

It is really not clear what you want if those two cases don't cover it. 如果这两种情况都无法解决,您真的想要什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM