reactive programming for java developers
play

Reactive Programming for Java Developers Rossen Stoyanchev About - PowerPoint PPT Presentation

Reactive Programming for Java Developers Rossen Stoyanchev About Me Spring Framework committer Spring MVC, WebSocket messaging Spring 5 Reactive Long-Running Shift to Concurrency Today Independent services, Cloud


  1. Reactive Programming for Java Developers Rossen Stoyanchev

  2. About Me Spring Framework committer ❖ Spring MVC, WebSocket messaging ❖ Spring 5 Reactive ❖

  3. Long-Running Shift to Concurrency

  4. Today Independent services, Cloud environment, 10 years ago Distributed apps Self-sufficient apps, App server, Keep it simple, don’t distribute

  5. Changing expectations Internet scale & resilience , Efficient use of resources, Latency is common

  6. Impact on programming model Imperative logic not so simple when latency is the norm Forced to deal with asynchronicity Limits of scale

  7. There is another way Fundamentally async & non-blocking Using very few threads Major shift but also major benefits

  8. Reactive Programming?

  9. In this talk How would we design an async API in Java ? Can we do better ? Introducing reactive libraries Spring reactive experience

  10. Design async API in Java

  11. Return one thing ... ...

  12. Usage

  13. Return it async style ... May occur in different thread

  14. Usage Ugh

  15. CompletableFuture (JDK 1.8) Future with actions ❖ Actions trigger when Future completes ❖ Callback mechanism ❖

  16. Return it async style with Java 1.8 ... ...

  17. Usage Async callback!

  18. Usage Requires null check

  19. Return many ... ...

  20. Return many ... ... No callback till all users collected

  21. Return many ... ... It may be too many

  22. Return nothing ... ...

  23. Return nothing ... ... Async notification: success or failure?

  24. Can we do better?

  25. Async results as a stream One notification per data item ❖ One notification for either completion or error ❖

  26. Return Type Description Notifications Success void onComplete() Failure void onError(Throwable) Match User onNext(User), onComplete() No match User onComplete() Failure User onError(Throwable) Two matches List<User> onNext(User), onNext(User), onComplete() No match List<User> onComplete() Failure List<User> onError(Throwable)

  27. Stream abstraction Functional, declarative programming model ➢ Combine, transform, reduce sequences ➢ Focus on what, not how ➢

  28. Java 8 Stream Great example of the benefits of a stream API ➢ However built for collections mainly ➢ Pull-based, usable once ➢

  29. Beyond collections Latency-sensitive data streams ➢ Infinite sequences ➢ Push-based notifications ➢

  30. Reactive Libraries

  31. Reactive library? Stream-like API similar to Java 8 ➢ Suited for any data sequence ➢ Latency-sensitive, infinite, collections ➢

  32. Project Reactor Reactive Streams foundation for the JVM ➢ API similar to ReactiveX ➢ Easy to bridge to Java 8 Stream ➢

  33. Flux -- sequence of 0..N

  34. Mono -- sequence of 0..1

  35. Flux to Java Stream

  36. Mono to CompletableFuture

  37. More than a stream API Reactor is back-pressure ready ➢ Reactive Streams spec ➢ Producers must not overwhelm consumers ➢

  38. Reactive Streams Spec Industry collaboration ❖ Small API, rules, TCK ❖ Reactive interoperability across libraries ❖

  39. Reactive Streams included in Java 9 “No single best fluent async/parallel API. CompletionStage best supports continuation-style programming on futures, and java.util.stream best supports (multi-stage, possibly- parallel) "pull" style operations on the elements of collections. Until now, one missing category was "push" style operations on items as they become available from an active source.“ Doug Lea, from initial announcement

  40. Reactive Streams in Java 9 Interfaces in java.util.concurrent.Flow ❖ SubmissionPublisher ❖ standalone bridge to Reactive Streams Tie-ins to CompletableFuture and Stream ❖

  41. Reactive Streams API public interface Publisher<T> { void subscribe(Subscriber<? super T> subscriber); }

  42. Reactive Streams API public interface Subscriber<T> { void onSubscribe(Subscription sub); void onNext(T item); void onError(Throwable ex); void onComplete(); }

  43. Reactive Streams API public interface Subscriber<T> { void onSubscribe(Subscription sub); void onNext(T item); void onError(Throwable ex); void onComplete(); }

  44. Reactive repository

  45. Using the reactive repository

  46. Using the reactive repository Subscriber triggers flow of data

  47. Using the reactive repository Consume all data by default

  48. Output onSubscribe request(unbounded) onNext(User: Jason) onNext(User: Jay) ... onComplete()

  49. Usage Consume two at a time

  50. Output onSubscribe request(2) onNext(User: Jason) onNext(User: Jay) request(2) onNext(User: Joe) onNext(User: John) ...

  51. More on Reactor Currently 2.5 M4 (might change to 3.0 label) ❖ GA release scheduled for July ❖ Hands-on exercise, blog post series ❖

  52. Reactive Spring

  53. Reactive Spring MVC ?

  54. Annotated controllers

  55. Controller Methods

  56. Annotated controllers Spring MVC Spring Web Reactive

  57. ...

  58. Mono<Object> ...

  59. @MVC Spring MVC Spring Web Reactive Servlet API ???

  60. @MVC Spring MVC Spring Web Reactive Servlet API ??? Servlet Container ???

  61. @MVC Spring Web Reactive HTTP Servlet 3.1 Reactor I/O RxNetty Reactive Streams

  62. Spring Framework 5.0 M1 spring-reactive

  63. More Reactive Efforts

  64. Reactive Journey

  65. @rstoya05

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend