Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Reactive Programming in Spring 5

4,718 views

Published on

Presented at Dutch Spring Meetup on 19-11-2015.

Published in: Software

Reactive Programming in Spring 5

  1. 1. Reactive Programming in Spring 5 Arjen Poutsma Pivotal
  2. 2. About Arjen • Twenty years of experience in Enterprise Software Development • Joined SpringSource in 2005 • Development lead of Spring Web Services, Spring REST support, Spring Scala • Working on Spring 5
  3. 3. Agenda • Reactive Systems • Reactive Streams • RxJava • Reactor • Reactive Programming in Spring 5
  4. 4. Reactive Systems
  5. 5. Reactive Manifesto Responsive Message Driven ResilientElastic
  6. 6. Reactive Manifesto • Responsive • Rapid • Consistent • Resilient • Replication • Isolation • Elastic • Scaling • No bottlenecks • Message Driven • Async • Back-pressure
  7. 7. Reactive Streams
  8. 8. Reactive Streams Spec • Focus on Interoperability • No operators • Wide support • Akka,Vert.x, RxJava, Ratpack • JDK 9 Flow in java.util.concurrent
  9. 9. Publisher Subscriber
  10. 10. Publisher Subscribersubscribe
  11. 11. Publisher Subscriber
  12. 12. Publisher Subscriber
  13. 13. Publisher Subscriber onSubscribe Subscription
  14. 14. Publisher Subscriber Subscription
  15. 15. Subscription Publisher Subscriber
  16. 16. Subscription Publisher Subscriber request 2
  17. 17. Subscription Publisher Subscriber 2
  18. 18. Subscription Publisher Subscriber 2
  19. 19. Subscription Publisher Subscriber onNext 1
  20. 20. Subscription Publisher Subscriber 1
  21. 21. Subscription Publisher Subscriber onNext 0
  22. 22. Subscription Publisher Subscriber 0
  23. 23. Subscription Publisher Subscriber request 5
  24. 24. Subscription Publisher Subscriber 5
  25. 25. Subscription Publisher Subscriber 5
  26. 26. Subscription Publisher Subscriber onNext 4
  27. 27. Subscription Publisher Subscriber 4
  28. 28. Subscription Publisher Subscriber onNext 3
  29. 29. Subscription Publisher Subscriber 3
  30. 30. Subscription Publisher Subscriber onNext 2
  31. 31. Subscription Publisher Subscriber 2
  32. 32. Subscription Publisher Subscriber onComplete 2
  33. 33. Publisher Subscribersubscribe onSubscribe Subscription request onNext onComplete onError
  34. 34. Hot & Cold Cold Hot Passive Active Publish when subscribed Publish regardless Queue Mouse Movement
  35. 35. Operators • map, flatMap, take, subscribe, … • No Operators in Reactive Streams • Left to implementations
  36. 36. Reactor
  37. 37. Project Reactor • Stream extends Publisher • Wrap Publisher
  38. 38. Reactor Streams.just('a' 'b' 'c') .take(2) .map(Character::toUpperCase) .consume(System.out::println);
  39. 39. RxJava
  40. 40. RxJava • Based On Reactive Extensions • Ported to many languages • Rx.NET, RxJava, RxJS, ... • Native support for Reactive Streams coming in 2.x
  41. 41. RxJava Observable.just('a', 'b', 'c') .take(2) .map(Character::toUpperCase) .subscribe(System.out::println)
  42. 42. RxNetty • RxJava on top of Netty • Client/server for TCP and HTTP • Backpressure support
  43. 43. Spring 5
  44. 44. Spring Reactive • Experimental work for Spring 5 • Non-blocking runtimes: • Netty, Jetty,Tomcat, (Undertow) • RxJava, Reactor https://github.com/spring-projects/spring-reactive
  45. 45. public interface ServerHttpRequest { HttpMethod getMethod(); URI getURI(); InputStream getBody(); } public interface ServerHttpResponse { void setStatusCode(HttpStatus status); OutputStream getBody(); } HttpMessage
  46. 46. public interface ReactiveServerHttpRequest { HttpMethod getMethod(); URI getURI(); Publisher<ByteBuffer> getBody(); } public interface ServerHttpResponse { void setStatusCode(HttpStatus status); Publisher<void> setBody(Publisher<ByteBuffer> body); } Reactive HttpMessage
  47. 47. Reactive Wep App Web Service Data
  48. 48. Wanted: Reactiveness • JDBC • MySQL • Postgres • NoSQL • MongoDB • HTTP Client • OkHttp • Spring 5 • …
  49. 49. Reactor @RequestMapping("/capitalize") @ResponseBody public Stream<Person> capitalize( @RequestBody Stream<Person> persons) { return persons.map(person -> { person.setName(person.getName().toUpperCase()); return person; }); }
  50. 50. RxJava @RequestMapping("/capitalize") @ResponseBody public Observable<Person> capitalize( @RequestBody Observable<Person> persons) { return persons.map(person -> { person.setName(person.getName().toUpperCase()); return person; }); }
  51. 51. More Spring! Spring Meetup http://www.meetup.com/Dutch-Spring- Meetup/ Spring Track GOTO Amsterdam 2016 http://gotoams.com
  52. 52. Q & A https://github.com/spring-projects/spring-reactive

×