如何从PublishSubject返回

时间:2019-10-29 17:07:50

标签: rx-java rx-java2 vert.x

在Vert.x中,我有一个发布主题,希望多个观察者可以订阅。 Vert.x期望得到一个CompletionStage。如何在不阻塞的情况下从观察者发送值?这是我要完成的工作的一个简单例子。

例如:

class TestClass {
  private PublishSubject ps = PublishSubject.create();

  public TestClass() {
    init();
  }

  public Observable<Foo> init() {
    Observable<Foo> foo = getDataFromInputStream();
    foo.subscribe(new Subscriber<Foo> () {
      public void onNext(Foo foo) {
        // Basically copy all values to a PublishSubject
        // since we are dealing with a Java Input Stream
        ps.onNext(foo);
      }
      public void onError(Throwable t) {
        ps.onError(t)
      }

      // onComplete is never called since the stream lives on 'forever'
      public void onComplete() {
      }
    });
  }

  public DataFetcher<CompletionStage<Foo>> vertxCallOne() {
    return env -> doWork().to(SingleInterop.get());
  }

  public DataFetcher<CompletionStage<Bar>> vertxCallTwo() {
    return env -> doOtherWork().to(SingleInterop.get());
  }

  public Observable<Foo> doWork() {
    ps.filter(blahBlahBlah)
    .subscribe(new Subscriber<Foo> () {
        public void onNext(Foo foo) {
        }

        public void onError(Throwable t) {
        }

        public void onComplete() {
        }
    });
    return ???
  }

  public Observable<Bar> doOtherWork() {
    // Reuse the same PublishSubject as doWork(), but with 
    // different filter/mapping rules.
    ps.filter(blah)
    .filter(blah2)
    .map()
    .subscribe(new Subscriber<Bar> () {
        public void onNext(Bar bar) {
        }

        public void onError(Throwable t) {
        }

        public void onComplete() {
        }
    });
    return ???
  }

任何帮助将不胜感激。谢谢!

0 个答案:

没有答案