如何等待异步Observable完成

时间:2014-06-18 15:10:00

标签: java rx-java

我正在尝试使用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)完成?

3 个答案:

答案 0 :(得分:17)

您的大多数示例都可以使用标准的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()的最后才有必要阻止。

答案 1 :(得分:12)

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

BlockingObservable.from(reviewService.findReviewsByItem(..)).toIterable()

答案 2 :(得分:-5)

另一种方法是在开始之前声明CountdownLatch。然后在onCompleted()上的那个锁存器上调用countDown()。然后,您可以使用该锁存器上的await()替换Thread.sleep()。