Blocking o X waits for Y • Non-Blocking o X returns and work, polling Y results Type of Operations • Synchronous - o X waits for Y • Asynchronous o X return before finish, can operate and won't come back until Y calls X o Callbacks, Future/Promises
From Microsoft Reactive extension library, Netflix developed the RX model for internal work •Twitter, Netflix, Red Hat and few more Corp. established Reactor Stream Org and declared a common model; The Reactive Manifesto • Reactive programming is a style micro-architecture involving intelligent routing and consumption of events, all combining to change behavior • FRP has strong affinity with high-performance, concurrency, asynchronous operations and non blocking I/O •Use cases: External service calls, high concurrency message consumers •Libraries/Frameworks: Reactive Streams, RxJava, Akka, Reactor
data.It optionally provides for 2 terminal events: error and completion. • Subscriber: A consumer of a data sequence. It receives a subscription on initialization to request how many data it wants to process next. • Subscription: A small tracker passed on initialization to the Subscriber. It controls how many data we are ready to consume and when do we want to stop consuming (cancel). • Processor: A marker for components that are both Subscriber and Publisher!
operators until to source operator 1->2->3->4->5 • Once signal reach the source it triggers it and start generating the data • Data starts to flow from top to bottom (execution time) 5>4>3>2>1 and print the emitted item
to an Executor Service. •Two main ways of switching execution context on reactor; • PublishOn • SubescribeOn •Both take schedulers and switch execution context to that scheduler. •Placement on PublishOn on chain matters while Subscribeon does not.
happens •More like an Executor service with workers (not same) •Workers schedule tasks •Multiple schedulers available on reactor •Schedulers.new*** returns a new instance Immediate • An empty scheduler object. Not switching threads Single • Task runs on unique executor service Elastic • For long lived tasks (IO), spawn threads without a limit Bounded Elastic • Same as elastic, but spawn limited threads Parallel • For short living CPU intensive tasks. Can execute N tasks parallel (N = # CPUs)
and Schedulers (spring.io) 1. Subscribe called on current thread, but subscription rapidly switched to subscriptionOn thread 2. Operator 1 & 2 executed on Scheduler used on SubscriberOn 3. Found publishOn, context switching and Operator 4 and subscribe consumer execute on that thread
possibly subsequent operators after the next one) to run on a different thread. •SubscribeOn forces the previous operator (and possibly operators prior to the previous one) to run on a different thread. •Once you switch the threads, you can't go back to MAIN thread again. (Java can't submit tasks to main threads)
streams. •Errors are terminal events. • When an error occurred , it terminates sequence and propagate down the chain of operators to last subscriber and to its onError method. • When error occurred, even error handling was used, it does not continue the original sequence • Converts to onError signal and propagate as a new sequence by replacing the current.
(Catch and return a static default value) onErrorResume • Instead of single fallback value, handle the error safer way. (Catch and execute an alternative path with a fallback method) onErrorMap • Catch an error and rethrow it with a business exception doOnError • React to the error without changing the sequence. (Catch, log an error-specific message, and re-throw) doFinally • Equals to finally block. Read more: Error Handling Operators
the previous slides, upstream get canceled and new Error stream is propagated down the sequence. •But we can RETRY if we face an error •Retry() will re-subscribe to upstream publisher. • It make a new sequence (original sequence is terminated) and re-run the publisher chain. •RetryWhen() gives ability to control the retry conditions Read more: Retry Operations
Landscape (spring.io) •Flight-of-the-flux •Reactor by Example (infoq.com) •Avoiding reactor meltdown (infoq.Com) •Projectreactor-dot-io-reactor3-intro •Presentation: reactive: do. Or do not. There is no try. - Speaker deck •Project reactor release docs •Asynchronous programming. Cooperative multitasking - blog | luminousmen •To learn reactive concepts (rxjava) •RX flow. My flow of learning rx | medium •Flight of the flux 2 - debugging caveats (spring.Io) •Intro to reactor core | baeldung •Applying Reactive Programming to Existing Applications - Reactive Programming with RxJava [Book] (oreilly.com)