Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Building RESTful Web Services with Spring 5

You're reading from   Building RESTful Web Services with Spring 5 Leverage the power of Spring 5.0, Java SE 9, and Spring Boot 2.0

Arrow left icon
Product type Paperback
Published in Jan 2018
Publisher Packt
ISBN-13 9781788475891
Length 228 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Ludovic Dewailly Ludovic Dewailly
Author Profile Icon Ludovic Dewailly
Ludovic Dewailly
Raja CSP Raman Raja CSP Raman
Author Profile Icon Raja CSP Raman
Raja CSP Raman
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. A Few Basics FREE CHAPTER 2. Building RESTful Web Services in Spring 5 with Maven 3. Flux and Mono (Reactor Support) in Spring 4. CRUD Operations in Spring REST 5. CRUD Operations in Plain REST (Without Reactive) and File Upload 6. Spring Security and JWT (JSON Web Token) 7. Testing RESTful Web Services 8. Performance 9. AOP and Logger Controls 10. Building a REST Client and Error Handling 11. Scaling 12. Microservice Basics 13. Ticket Management – Advanced CRUD 14. Other Books You May Enjoy

Imperative and Reactive programming

Let's see a small comparison between Imperative programming and Reactive programming: x = y + z.

In the preceding expression, assume y = 10 and z = 15. In this case, the x value would be 25. The value of x would be assigned at the time of the expression x = y + z. The value of x will never change after this expression.

This is perfectly alright in the traditional programming world. However, we might need a scenario where we should be able to follow up x when we change the value of y or z.

Our new scenario based values are:

  • When y = 20 and z = 15, then x = 35
  • When y = 20 and z = 25, then x = 45

The preceding scenario is not possible in Imperative programming, which we regularly use in our daily programming. But in some cases, we might need the value of x to be updated, corresponding to the change in y or z. Reactive programming is the perfect solution for this scenario. In Reactive programming, the value of x would automatically be updated, corresponding to the change in y or z.

Spreadsheet reference cells are the best example of Reactive programming. If a cell value changes, the referred cell value will be updated automatically. Another example can be found in a Model-View-Controller architecture, Reactive programming can automatically update the View, which is attached to the Model.

Reactive programming follows the Observer pattern to manipulate and transform the stream of data where the Publisher (observable) emits the items based on the Subscriber's need. As the Publisher emits the item, the Subscriber consumes those emitted items from the Publisher. Unlike the iterator pulling the items, here, the Publisher is pushing the items to the Subscriber.

As Reactive is a part of non-blocking architecture, it will be useful when we scale the application. Also, in non-blocking architecture, everything is considered as an event stream.

We will discuss more about Reactive in Java and Spring later in this chapter.

Reactive Streams

Reactive Streams are all about processing an asynchronous stream of data items, where applications react to data items as they receive them. This model is more memory-efficient, as it doesn't rely on any in-memory data.

Reactive Streams have four main components:

  1. Publisher.
  2. Subscriber.
  3. Subscription.
  4. Processor.

The Publisher publishes a stream of data, to which the Subscriber is asynchronously subscribed. The Processor transforms the data stream without the need for changing the Publisher or the Subscriber. The Processor (or multiple Processors) sits between the Publisher and the Subscriber to transform one stream of data to another.

Benefits of Reactive programming

The Reactive Streams approach is supported by engineers at Netflix, Pivotal, Twitter, Oracle, and TypeSafe. Especially, TypeSafe contributed more to Reactive Streams. Even Netflix engineers say, in their own words:

“Reactive programming with RxJava has enabled Netflix developers to leverage server-side concurrency without the typical thread-safety and synchronization concerns.”

The following are the benefits of Reactive programming:

  • Focuses on business logic
  • Stream processing causes memory efficiency
  • Overcomes low-level threading, synchronization, and concurrency issues

Reactive principles are used in real-time cases such as live database queries, big data, real-time analytics, HTTP/2, and so on.

You have been reading a chapter from
Building RESTful Web Services with Spring 5 - Second Edition
Published in: Jan 2018
Publisher: Packt
ISBN-13: 9781788475891
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime