Showing posts with label Reactive Programming. Show all posts
Showing posts with label Reactive Programming. Show all posts

July 01, 2019

What's New in Spring Framework 5?

What are the new features added in Spring 5?
#Java Baseline Support
  • Spring framework 5.0 codebase runs on Java 8. Therefore, Java 8 is the minimum requirement to work on Spring Framework 5.0. It also supports java 9.
  • Spring 5 supports Java EE 7 and also compatible with Java EE 8. We can use Servlet 4.0, Bean Validation 2.0, JPA 2.2 in our applications. We can also use their older versions i.e. Servlet 3.1, Bean Validation 1.1, JPA 2.1.
  • Spring 5 applications preferred server versions are Tomcat 8.5+, Jetty 9.4+ and WildFly 10+.

January 11, 2019

#RxJava Part 5 : map() vs flatMap()

What is the difference between map() and flatMap()?
The map() method works well with Optional if the function returns the exact type we need. e.g:

Optional s = Optional.of("employee");

But in more complex cases we might be given a function that returns an Optional too. In such cases using map() would lead to a nested structure, as the map() implementation does an additional wrapping internally.

assertEquals(Optional.of(Optional.of("STRING")), Optional.of("string") .map(s -> Optional.of("STRING")));

#RxJava Part 4: Operators in RxJava

An operator is a function that takes one Observable as a source as its first argument and returns another Observable as the destination. For every item that the source observable emits, it will apply a function to that item, and then emit the result on the destination Observable.

When we want to create complex data flows that filter event based on certain criteria, we can chain the operators one after another. i.e we can apply multiple operators to the same observable.

#RxJava Part 3 : from(), just(), range(), interval(), timer()

Observable.from() Example
In the below example, the from() method dissolves the list/array and emits each value one at a time.

GIT URL: RxJavaDemoFrom.java
Observable.just()
The Observable.just() will emit whatever is present inside the just function. It can take between 2 to 9 parameters, we can pass a List/Array in it and it’ll emit the List/Array only.

GIT URL: RxJavaDemoJust.java

Observable.range()
Observable.range(start,n) methiod is used to emit 'n' number of values starting from and inclusive of start. e.g:

Observable rangeObservable = Observable.range(3,5);
rangeObservable.subscribe(intSubscriber); //emits 3,4,5,6,7
Observable.empty() //creates an empty observable that emits nothing. It just completes.
Observable.error() //creates an error. The onError() of all the subscribers would be called.
Observable.never() //does nothing. Neither emits a complete nor an error.

Observable.interval()
Observable.interval() emits constant sequences of integers in ascending order which are evenly spaced by the interval specified. e.g: Below code will emit 0 to 4 each second. We’ve set the thread to sleep to prevent the main function from returning immediately.

Observable intervalObservable = Observable.interval(1, TimeUnit.SECONDS);
intervalObservable.subscribe(System.out::println);
Thread.sleep(5000);

Observable.timer()
Unlike interval, an Observable.timer() emits value only after a certain time/delay. e.g: below code will emit only a single value after the delay.

Observable intervalObservable = Observable.timer(2, TimeUnit.SECONDS);
intervalObservable.subscribe(System.out::println);
Thread.sleep(5000);

Observable.defer()
The Observable.defer() is similar to create() except that it postpones the actual creation until an Observer subscribes. Each subscription would recall the Observable creation. This ensures that the Observer would always receive the latest data. It also ensures that no API call occurs until Subscription. The data would be only fetched when required by the observer. e.g:

Observable deferObservable = Observable.defer(() -> Observable.just(1, 2, 3));

-K Himaanshu Shuklaa..

#RxJava Part 2 : Creating Observable, Observers and Subscribers

Creating Observers and Subscribers
While creating Observers and Subscribers, we need to override three methods:
  • onNext(): It gets the current value. It is called on observer each time a new event is published to the attached Observable. This is the method where we'll perform some action on each event.
  • onComplete(): It gets triggered when there is no more data left to be sent by the observable. This method indicate that we should not expect any more onNext calls on our observer
  • onError(): It gets triggered in case an exception  is thrown during the RxJava framework code or our event handling code.
FYI, the Iterator does have the equivalents for onNext() and onComplete() (hasNext()). It doesn’t have one when an exception is thrown. This is another advantage of Reactive code.

#RxJava Part 1

Reactive Programming
  • In reactive programming, we react to changes in the state instead of actually doing the state change.
  • The reactive model listens to changes in the event and runs the relevant code accordingly.
  • e.g if in an excel file we have three columns A, B and C. In the C column we added a formula because of which value in C is A+B. All the rows of column C is populated with the summation of values in A and B. Now if we change the formula from A+B to A-B, it will automatically change the values in all the rows of column C.
  • Reactive Programming is a programming paradigm that’s concerned with data streams and propagation of change.
  • Assume the data streams are in the form of a river that flows continuously. Any observer/subscriber attached listening to the stream would receive the data. The data received can be further transformed using functions and this is where Functional Programming joins the already so powerful Reactive Programming.
  • In reactive programming, the flow is asynchronous thereby preventing any blocks on the main thread.