没有合适的资源?快使用搜索试试~ 我知道了~
首页hands-reactive-programming-spring-5.pdf
Nowadays,reactiveis a buzzword—so exciting but so confusing. However, should we still care about reactivity even if it takes an honorable place in conferences around the world? If we google the word reactive, we will see that the most popular association is programming
资源详情
资源评论
资源推荐
Page 1
hands-reactive-programming-spring-5
Why Reactive Spring?
In the previous section, we at a few interesting examples in which reactive system approaches shine.We have also expanded on looked
the usage of fundamentals such as elasticity and resilience, and seen examples of microservice-based systems commonly used to attain
a reactive system.
That gave us an understanding of the architectural perspective but nothing about the implementation. However, it is important to
emphasize the complexity of the reactive system and the construction of such a system is a challenge. To create a reactive system with
ease, we have to analyze frameworks capable of building such things first and then choose one of them. One of the most popular ways
to choose a framework is by analyzing its available features, relevance, and community.
In the JVM world, the most commonly known frameworks for building a reactive system has been Akka and Vert.x ecosystems.
On the one hand, Akka is a popular framework with a huge list of features and a big community. However, at the very beginning, Akka
was built as part of the Scala ecosystem and for a long time, it showed its power only within solutions written in Scala. Despite the fact
that Scala is a JVM-based language, it is noticeably different from Java. A few years ago, Akka provided direct support for Java, but for
some reason, it was not as popular in the Java world as it was in Scala.
On the other hand, there is the Vert.x framework which is also a powerful solution for building an efficient reactive system. Vert.x was
designed as a non-blocking, event-driven alternative to Node.js that runs on the Java Virtual Machine. However, Vert.x started being
competitive only a few years ago and during the last 15 years, the market for frameworks for flexible robust application development has
been held by the Framework. Spring
Note
To get more information about the Java tools landscape, follow this link: https://www.quora.com/Is-it-worth-learning-Java-
Spring-MVC-as-of-March-2016/answer/Krishna-Srinivasan-6?srid=xCnf (https://www.quora.com/Is-it-worth-learning-
.Java-Spring-MVC-as-of-March-2016/answer/Krishna-Srinivasan-6?srid=xCnf)
The Spring Framework provides wide possibilities for building a web application using a developer-friendly programming model.
However, for a long time, it had some limitations in building a robust reactive system.
Reactivity on the service level
Fortunately, the growing demand for reactive systems initiated the creation of a new Spring Project called . The Spring Spring Cloud
Cloud Framework is a foundation of projects that address particular problems and simplifies the construction of distributed systems.
Consequently, the Framework ecosystem may be relevant for us to build reactive systems.Spring
Note
To learn more about the essential functionality, components, and features of that project please click on the following link: http://
.projects.spring.io/spring-cloud/ (http://projects.spring.io/spring-cloud/)
Page 2
hands-reactive-programming-spring-5
We will skip the details of Spring Cloud Framework functionality in this chapter and cover the most important parts that help in the
development of the reactive system in , Chapter 8 (/mapt/book/application_development/9781787284951/8) Scaling Up with Cloud
Nonetheless, it should be noticed that such a solution building a robust, reactive microservices system with minimum effort.Streams.
However, the overall design is only one element of constructing the whole reactive system. As may be noticed from the excellent
Reactive Manifesto:
"Large systems are composed of smaller ones and therefore depend on
the Reactive properties of their constituents. This means that Reactive
Systems apply design principles so these properties apply at all levels
of scale, making them able to be composed".
Therefore, it is important to provide a reactive design and implementation on the component level as well. In that context, the
term design principle refers to a relationship between components and, for example, programming techniques that are used to
compound elements. The most popular traditional for writing code in Java is .technique imperative programming
To understand whether imperative programming follows reactive system design principles, let's consider the next diagram:
Page 3
hands-reactive-programming-spring-5
Diagram 1.6. UML Schema of component relationship
Here, we have two components within the web store application. In that case, callsOrdersService ShoppingCardService
while processing the user request. Suppose that under the hood executes a long-running I/O operation, ShoppingCardService
for example, an HTTP request or database query. To understand the disadvantages of imperative programming let's consider the
following example of the most common implementation of the aforementioned interaction between components:
The aforementioned code is explained as follows:
As we can understand from the preceding code, in Java world, the execution of blocks the scService.calculate(input)
on which the processing of the logic takes place. Thus, to run a separate independent processing Thread OrdersService
in we have to allocate an additional .As we will see in this chapter, the allocation of an additional OrderService Thread
might be wasteful. Consequently, from the reactive system perspective, such system behavior is unacceptable.Thread
Note
Blocking communications directly contradicts the message-driven principle, which explicitly offers us non-blocking
communication. See the following for more information on this: https://www.reactivemanifesto.org/#message-driven (https://
.www.reactivemanifesto.org/#message-driven)
Nonetheless, in Java, that problem may be solved by applying a callback technique for the purpose of cross-component communication:
Copy
interface ShoppingCardService { // (1)
Output calculate(Input value); //
} //
class OrdersService { // (2)
private final ShoppingCardService scService; //
//
void process() { //
Input input = ...; //
Output output = scService.calculate(input); // (2.1)
... // (2.2)
} //
} //
This is the interface declaration. This corresponds to the aforementioned class diagram and has ShoppingCardService
only one method, which accepts one argument and returns a response after its processing.calculate
1
This is the declaration. Here, at point we synchronously call OrderService (2.1) ShoppingCardService
and receive a result right after its execution. Point hides the rest of the code responsible for result processing.(2.2)
2
In turn, in that case our services are tightly coupled in time, or simplythe execution of is tightly coupled to OrderService
the execution of . Unfortunately, with such a technique, we cannot proceed with any other ShoppingCardService
actions while is in the processing phase.ShoppingCardService
3
Copy
Page 4
hands-reactive-programming-spring-5
Each point in the preceding code is explained in the following numbered list:
Now, passes the function-callback to react at the end of the operation. This embraces the fact thatOrdersService
is now decoupled from and the first one may be notified via the functional callback OrdersService ShoppingCardService
where the implementation of the method, which calls the given function, may ShoppingCardService#calculate
either be synchronous or asynchronous:
Each point in the preceding code is explained in the following numbered list:
interface ShoppingCardService { // (1)
void calculate(Input value, Consumer c); //Output< >
} //
class OrdersService { // (2)
private final ShoppingCardService scService; //
//
void process() { //
Input input = ...; //
scService.calculate(input, output -> { // (2.1)
... // (2.2)
}); //
} //
} //
The preceding code is the interface declaration. In that case, the ShoppingCardService calculate
method accepts two parameters and returns a void. It means that from the design perspective, the caller may be immediately
released from waiting and the result will be sent to the given callback later.Consumer<>
1
This is the declaration. Here, at point we asynchronously call OrderService (2.1)
and continue processing. In turn, when the executes the callback ShoppingCardService ShoppingCardService
function we will be able to proceed with the actual result processing .(2.2)
2
Copy
class SyncShoppingCardService implements ShoppingCardService { // (1)
public void calculate(Input value, Consumer c) { //Output< >
Output result = new Output(); //
c.accept(result); // (1.1)
} //
} //
class AsyncShoppingCardService implements ShoppingCardService{ // (2)
public void calculate(Input value, Consumer c) { //Output< >
new Thread(() -> { // (2.1)
Output result = template.getForObject(...); // (2.2)
... //
c.accept(result); // (2.3)
}).start(); // (2.4)
} //
} //
This point is the class declaration. This implementation the absence of blocking SyncShoppingCardService assumes
Page 5
hands-reactive-programming-spring-5
In that example, we have the sync implementation of , which keeps synchronous bounds and offers no ShoppingCardService
benefits from the API perspective. In the async case, we achieve asynchronous bounds, and a request will be executed in the separate
. is decoupled from the execution process and will be notified of the completion by the callback Thread OrdersService
execution.
The advantage of that is that components are decoupled in time by the callback function. This means that after calling the technique
method, we will be able to proceed with other operations immediately without waiting for the response in scService.calculate
the blocking fashion from .ShoppingCardService
The disadvantage is that callback requires the developer to have a good understanding of multi-threading to avoid the traps of shared
data modifications and callback hell.
Note
Actually, the phrase is mentioned in relation to JavaScript: , but callback hell http://callbackhell.com (http://callbackhell.com)
it is also applicable to Java as well.
Fortunately, the callback technique is not the only option. Another one is which, to some java.util.concurrent.Future ,
degree, hides the executionalbehavior and decouples components as well:
The numbered points are described in the following:
This point is the class declaration. This implementation the absence of blocking SyncShoppingCardService assumes
operations. Since we do not have an I/O execution, the result may be returned immediately by passing it to the callback
function .(1.1)
1
This point in the preceding code is the class declaration. In the case, when we have AsyncShoppingCardService
blocking I/O as depicted in point , we may wrap it in the separate . After (2.2) Thread (2.1) (2.4)
retrieving the result, it will be processed and passed to the callback function.
2
Copy
interface ShoppingCardService { // (1)
Future calculate(Input value); //Output< >
} //
class OrdersService { // (2)
private final ShoppingCardService scService; //
//
void process() { //
Input input = ...; //
Future future = scService.calculate(input); // (2.1)Output< >
... //
Output output = future.get(); // (2.2)
... //
} //
} //
At this point is the interface declaration. Here, the method accepts one ShoppingCardService calculate
parameter and returns . is a class wrapper which allows us to check whether there is an available Future Future
result or blocking to get it.
1
This is the declaration. Here, in point , we asynchronously call OrderService (2.1)
and receive the instance. In turn, we are able to continue processing while the ShoppingCardService Future
2
剩余360页未读,继续阅读
oneF
- 粉丝: 0
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
- SPC统计方法基础知识.pptx
- MW全能培训汽轮机调节保安系统PPT教学课件.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0