Advertisement
Guest User

Untitled

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