Skip to content

Whats New in Spring5 Reactive

Andrew Serff edited this page Dec 22, 2017 · 1 revision

#What's new in Spring 5

SpringOnePlatform 2017

sashin@pivotal.com @javapassion

Reactive Fundimentals

Distributed Microservices architectures inheriently introduce latency, require redundancy and recovery, and the need to scale out, not up.

Reactive Manifesto:

  • Responsive
  • Resilient
  • Elastic
  • Message Driven

What is Reactive Programming:

  • Non-Blocking, asyncyronous and event driven.
  • Flow control (backpressure)
  • Programming with async data streams

Event driven architectures have been around for a long time.

Backpressure - Don't over burden a component.

Reactive Streams

Require a Publisher and Subscriber. Subscriber subscribes to a Publisher and can then tell it how much data it can handle. You don't get data until you request(#) as a subscriber.

onNext - called when there is data onError - called on error onComplete - called when the data stream is complete (no more data will be available)

Implementations:

  • Java 9
  • Project Reactor

Any of the operators that end in With will return a new Producer.

Flux is lazy and won't produce any thing in the stream until there is a subscriber.

How it's different than Java 8 Streams:

  • Java 8 streams is a pull model. Reactive Streams are a push model. You don't have to wait until ALL the data is there.

Reactive Repositories

Mongo/Redis/Cassandra have reactive repositories

The repositories are non-blocking.

  1. Mono updateMovie(int id, Mono movie); vs

  2. Mono updateMovie(int id, Movie movie); vs

  3. Movie updateMovie(int id, Mono movie);

  4. Provide a Mono as an argument if you don't immediately have the Movie available

  5. Provide a Movie as an argument if there is no latency added by getting a movie object to update.

  6. Return a Mono so the client doesn't have to wait. They will get a Movie when it is available.

You need to remember that the Publisher and Subscriber are running in different threads. With Reactive programming, you are writing multi-threaded code without any threading.

Web Flux

Why?

  • Handle concurrency with a smaller number of threads
  • Scale with less hardware
  • Need to leverage functional programming apis to give declarative composition or async logic.

2 choices:

  • Annotation based (same as before)
  • Functional endpoints

Reactive and non-blocking doesn't necessiarly make your application faster.

WebTestDriver

flapmongo: https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo fongo: https://github.com/fakemongo/fongo

Clone this wiki locally