Mabimal » Blog Archives

Tag Archives: java

Java Programming Reactive

Reactive programming

General Speaking

  • Reactive is used broadly to define event-driven systems
  • UI events, network protocols
  • Now also entering the domain of application/business logic
  • Non-blocking event-driven architecture

Demo1: Demo Simple Reactive can be an excel file as well, if we change the one cell value then all other cell gets updated. This is reactive in its simplest form.

Myths about “Reactive”

  • Async/concurrent == reactive?
    • Easy to end up with too many threads
    • Fill up hand-off queue
  • Must be async to be reactive?
    • Nope but must be agnostic to source of concurrency
  • Reactive is the domain of specific programming languages?
    • Language features can help
    • E.g. JDK 8 Lambda

Reactive Specification Goals

  • Govern the exchange of data across async boundaries
  • Use back-pressure for flow control
  • Fast sources should not overwhelm slower consumers
  • Provide API types, TCK (Technology Compatibility Kit)

Back-Pressure

  • When publisher maintains higher rate for extended time
    • Queues grow without bounds
  • Back-pressure allows a subscriber to control queue bounds
    • Subscriber requests # of elements
    • Publisher produces up to # of requested
  • If source can’t be controlled
    • Publisher may buffer or drop
    • Must obey # of requested elements
Subscriber controlled flow: Instead of publisher publishing n number of streams, subscriber request how much it can consume at a time and then publisher publishes
All methods are void, meaning they are non-blocking

Best Reactive Frameworks Available in Java

List of available reactive frameworks in java being used in non-trivial applications.

  • Eclipse Vert.x
  • Atmosphere
  • Spring Webflux
  • RxJava
  • RatPack
  • Project Reactor
  • Akka

Demo 2: IntelliJ based project with basics concepts

In JDK 9

  • JDK9 java.util.concurrent.Flow
  • The interfaces available in JDK9’s java.util.concurrent.Flow, are 1:1 semantically equivalent to their respective Reactive Streams counterparts

Spring WebFlux

Spring 5 has embraced reactive programming paradigm by introducing a brand new reactive framework i.e spring Webflux.

It is asynchronous framework from the bottom up.

It can run on servlet containers using the servlet 3.1 non-blocking IO API.

Library used is reactor which is an implementation of reactive streams specification.

Reactor provides two main types called Flux and Mono. Both implement the Publisher interface provided by Reactive Streams.

Flux is used to represent a stream of 0..N elements and Mono is used to represent a stream of 0..1 element

Although spring uses Reactor as a core dependency for most of its internal APIs, it also supports RxJava at the application level.

Demo Spring Webflux : It uses mongodb as db, so in order to use it, please install mongodb locally.

References:

https://github.com/callicoder/spring-webflux-reactive-rest-api-demo

https://www.callicoder.com/reactive-rest-apis-spring-webflux-reactive-mongo/

https://www.reactive-streams.org/

https://github.com/rstoyanchev/s2gx2015-intro-to-reactive-programming

Published by: