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+.
#Core API Enhancements
  • @Nullable annotation: @Nullable and @NotNull annotations will explicitly mark nullable arguments and return values. This enables dealing null values at compile time rather than throwing NullPointerExceptions at runtime. Using @Nullable imposes an obligation on the consumers that they must prepare for a value to be null. Prior to this release, the only way to accomplish this is through Android’s Nullable, Checker Framework’s Nullable, and JSR 305’s Nullable.
  • Spring Framework 5.0 comes with its own Commons Logging bridge; spring-jcl instead of standard Commons Logging.
  • File operations are performed via NIO 2 streams i.e. no FileInput/Output Stream. This is a great enhancement and performance boost for file based applications.
#Spring WebFlux: This is the new module added in Spring 5, it’s an alternative to spring-webmvc module and built on reactive framework. This module is used to create fully asynchronous and non-blocking application built on event-loop execution model.

#Testing Improvements
  • Support for JUnit 5. 
  • Also the support for parallel test execution in the Spring TestContext Framework
#Deprecated Support and Removed Packages
  • BeanFactoryLocator: It comes from the package beans.factory.access has been actually removed from Spring. This is often used in layered application contexts. Most of the times we see this interior architectures of EJB applications. The solution to this problem to either move away from EJB or if it's not possible leverage EJB 3.1. With EJB 2.1, we can use CDI, which should replace the need for the BeanFactoryLocators. 
  • NativeJdbcExtractors: This comes from the package jdbc.support.nativejdbc has been also removed in Spring 5.0. This was used to extract native objects from the JDBC abstraction, e.g : raw connections, native result sets, etc. The construct that has now been provided, is the connection.unwrap() method.
  • AnnotationDrivenStaticEntityMockingControl: This was used for static mocks. It was used to aspect static entry of mock support in tests. There are many other libraries with which can do to do this same thing. 
  • Removed packages web.view.tiles2, and now you should be using Tiles 3. 
  • Hibernate, 3 and 4 were both removed in favor of Hibernate 5. 
  • Dropped support for old technologies Portlet, Velocity, JasperReports, XMLBeans, JDO (Java Data Objects), Guava. If you are using any of these, then either migrate to some other technologies or stay with Spring 4.
What is backpressure?
  • The Wikipedia definition of backpressure, "Resistance or force opposing the desired flow of fluid through pipes." In the context of software, its the resistance or force opposing the desired flow of data through software.
  • The purpose of any software is to take input data and return some desired output data. Backpressure is when the progress of turning that input to output is resisted in some way. 
  • e.g: Let's say we are using microservice architecture, where responsibilities are separated into multiple servers. Backpressure will occur when one server is sending requests to another faster than it can process them.
How can we handle backpressure?
  • Control the producer by slowing it down. Unfortunately, of course controlling the producer isn’t always possible.
  • Buffering can be used to accumulate incoming data spikes temporarily. However, always keep in mind that buffering is dangerous if unbounded — meaning you don’t have a size or time limit to the buffer. Unbounded buffers are a common source of memory crashes in servers.
  • Drop, sample a percentage of the incoming data. The most common is sampling based on time e.g. 10% of data per second.
What is Reactive Programming?
  • The Reactive Streams API is officially part of Java 9. In Java 8, you will need to include a dependency for the Reactive Streams API specification.
  • Streaming support in Spring Framework 5.0 is built upon Project Reactor, which implements the Reactive Streams API specification.
  • Reactive programming is an approach to writing software that embraces asynchronous I/O. The Reactive Programming manages asynchronous data flows between producers of data and consumers that need to react to that data in a non-blocking manner. 
  • We can say, Reactive Programming is all about non-blocking asynchronous, event-driven applications which require a small number of threads to scale.
  • With Reactive programming we can build systems that are resilient to high load.
  • Spring Framework 5.0 has a new spring-webflux module that supports reactive HTTP and WebSocket clients. Spring Framework 5.0 also provides support for reactive web applications running on servers which include REST, HTML, and WebSocket-style interactions.
Ideal use-cases for implementing Reactive programming?
  • In share trading business, where share prices fluctuate every now and then.
  • Notification services of large online shopping applications like Flipkart and Amazon. 
  • In Bank sector where we need a large number of transaction-processing services.
What is Reactive Streams?
Reactive Streams API is officially part of Java 9 as java.util.concurrent.Flow and allows the subscriber to control the data exchange rate from publishers.

Spring 5 offerings for Reactive Programming?
  • Like Spring MVC, the Spring-Web-Reactive module support the same @Controller programming, but additionally executes on a Reactive and non-blocking engine.
  • The Spring-Web-Reactive module has redefined many of the Spring MVC contracts, such as HandlerMapping and HandlerAdapter, to make them asynchronous and non-blocking as well as enabling reactive HTTP request and response in the form of RouterFunction and HandlerFunction.
  • In Spring 5, in addition to the existing RestTemplate the new reactive WebClient has also been introduced.
  • Spring 5 Framework introduced Reactor as an implementation for the Reactive Streams specification. It is a next-gen Reactive library for building non-blocking applications on the JVM.
  • HTTP clients (e.g. Reactor, Netty, Undertow) that support Reactive Programming have adapted to a set of reactive ClientHttpRequest and ClientHttpResponse abstractions that expose the request and response body as Flux with full backpressure support on the read and write side. Reactor is a next-gen Reactive library for building non-blocking applications on the JVM.
E.g:
  • Let's say we have a rest-controller, it has a webservice getUserDetails, which returns the user details based on user id.
  • From the thread prospective, whenever a new request is received, a new servlet thread will be assigned to it. This thread does processing, which might include a database call. We are fetching user details based on ID from DB, this is an IO operation between our web-server and database. During this IO call, the servlet thread will go into the waiting state. It will wait until, the details are fetched from DB and given back to the servlet thread. Once the thread get's the user details, it returns the response.
  • If a new request comes in, while the above servlet thread is in waiting state, then a new servlet thread will be assigned to this new request.
  • Now, lets say lots of concurrent requests are coming for each request a servlet thread will be created. There is typically a thread pool available to handle the incoming requests, the size servlet thread pool is usually 200. That means, at the most we can concurrently serve 200 requests, if a new request come it need to wait till the thread is available in the pool. 
  • To serve more number of requests more efficiently, we need a better mechanism. For this, whenever we get a request we assign a servlet thread to it, which will start processing and then it will do the IO operation. Instead of waiting for the IO operation to complete, it will ask the database driver to call the thread once the data is available. In the meanwhile, the servlet thread will go back and start serving new requests.
  • This call back or event based mechanism is called Event Loop.
  • In Event Loop, we will have a limited pool of thread, which are available to serve the requests. Each thread will perform the required operation, delegates the request to DB for IO operations. DB will take the event, do the IO operation, once it's done it will fire an event to intimate that the data is fetched from DB. Any of the threads available in the pool, takes that data and sends the response back. 
  • But this increases the complexity of the system, as someone has to check to which thread the IO event need to send data. This is handled by Spring Web Flux.
  • The Spring Web Flux handles end-to-end reactive or event based loop mechanism, so that we can use less number of threads and high CPU efficiency.
  • Internally Spring Web Flux uses, Servlets 3.0 and 3.1 (Async and NIO), Java NIO (File, Network IO), MongoDB, Cassandra, Redis.

-K Himaanshu Shuklaa..

No comments:

Post a Comment

RSSChomp Blog Directory