Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Nurkiewicz, Tomasz,Christensen, Ben. Reactive Programming with RxJava: Creating Asynchronous, Event-Based Applications (p. 131). O'Reilly Media. Kindle Edition.
- // Scenario
- // 1. The list can be potentially quite long
- // 2. Sending an email might take several millionseconds or even seconds
- // 3. The application must keep running gracefully in case of failures, but report in the end which tickets failed to delievered.
- List<Ticket> failures = Observable.from(tickets)
- .flatMap(ticket ->
- rxSendEmail(ticket)
- .flatMap(response -> Observable.<Ticket>empty())
- .doOnError(e -> log.warn("Failed to send {}", ticket, e))
- .onErrorReturn(err -> ticket))
- .toList()
- .toBlocking()
- .single()
- // above code collect tickets failed to be delievered
- // cf. ignoreElements() : ignores all emitted values and forward onCompleted or onError.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement