14

我正在尝试使用 rxjava 构建示例。该示例应协调 ReactiveWareService 和 ReactiveReviewService 重新运行 WareAndReview 组合。

ReactiveWareService
        public Observable<Ware> findWares() {
        return Observable.from(wareService.findWares());
    }

ReactiveReviewService: reviewService.findReviewsByItem does a ThreadSleep to simulate a latency!

public Observable<Review> findReviewsByItem(final String item) {
return Observable.create((Observable.OnSubscribe<Review>) observer -> executor.execute(() -> {
    try {
        List<Review> reviews = reviewService.findReviewsByItem(item);
        reviews.forEach(observer::onNext);
        observer.onCompleted();
    } catch (Exception e) {
        observer.onError(e);
    }
}));
}

public List<WareAndReview> findWaresWithReviews() throws RuntimeException {
final List<WareAndReview> wareAndReviews = new ArrayList<>();

wareService.findWares()
    .map(WareAndReview::new)
.subscribe(wr -> {
        wareAndReviews.add(wr);
        //Async!!!!
        reviewService.findReviewsByItem(wr.getWare().getItem())
            .subscribe(wr::addReview,
                throwable -> System.out.println("Error while trying to find reviews for " + wr)
            );
    }
);

//TODO: There should be a better way to wait for async reviewService.findReviewsByItem completion!
try {
    Thread.sleep(3000);
} catch (InterruptedException e) {}

return wareAndReviews;
}

鉴于我不想返回 Observable,我该如何等待异步 Observable (findReviewsByItem) 完成?

4

3 回答 3

18

您的大多数示例都可以使用可以很好地协同工作的标准 RxJava 运算符重写:

public class Example {

    Scheduler scheduler = Schedulers.from(executor);

    public Observable<Review> findReviewsByItem(final String item) {
        return Observable.just(item)
               .subscribeOn(scheduler)
               .flatMapIterable(reviewService::findReviewsByItem);
    }
    public List<WareAndReview> findWaresWithReviews() {
        return wareService
               .findWares()
               .map(WareAndReview::new)
               .flatMap(wr -> {
                   return reviewService
                          .findReviewsByItem(wr.getWare().getItem())
                          .doOnNext(wr::addReview)
                          .lastOrDefault(null)
                          .map(v -> wr);
               })
               .toList()
               .toBlocking()
               .first();
    }
}

每当您想编写这样的服务时,请flatMap首先考虑。您不需要为每个子 Observable 进行阻止,但仅在toBlocking()确实需要时才阻止。

于 2015-08-13T21:07:03.897 回答
13

您可以使用 BlockingObservable 中的方法,请参阅https://github.com/Netflix/RxJava/wiki/Blocking-Observable-Operators。例如

BlockingObservable.from(reviewService.findReviewsByItem(..)).toIterable()
于 2014-06-23T13:33:25.203 回答
-5

另一种方法是在开始之前声明一个 CountdownLatch。然后在 onCompleted() 中对该闩锁调用 countDown()。然后,您可以在该闩锁上将 Thread.sleep() 替换为 await() 。

于 2015-08-12T22:37:11.960 回答