Responsive programming Technical Overview

Reactive programming is the use of asynchronous data streams are programmed, is a form of observers (Observer) mode in nature. We first discuss several common ways to achieve asynchronous operation, and then leads to the realization of the mainstream reactive programming techniques.

1. The common way to implement asynchronous

In Java, in order to achieve non-blocking asynchronous, typically using a callback (Callback) and Future These two mechanisms, but both mechanisms there are some limitations.

(1) callback

Callback meanings shown below, i.e. class methodA () methodB method calls class B () method A, B, and Class methodB () then calls the method is finished active class A callback () method. Callback is embodied in a two-way invocation.

Callback can be seen in the task execution process will not cause any obstruction, the results once the task is ready, the callback will be executed. But we should also see when using the callback mechanism, the code jumps to a method in another class from a class a method, resulting in discontinuities process. For asynchronous execution of a single layer, the callback is easy to use. But for nested asynchronous multi-layer combinations that it is very clumsy. It is difficult to large-scale pullback in combination, because soon it will lead to code difficult to understand and maintain that form the so-called "hell callback (Callback Hell)" problem.

(2)Future

Future model can be understood as a simple scenario: We have a task processing of hope, and then submit the task to the Future, Future will complete this task within a certain time, and during this time we can do other thing. Future as an implementation mode, Java the Future interface only includes the following five methods.

public interface Future<V> {

    boolean cancel(boolean mayInterruptIfRunning);

    boolean isCancelled();

    boolean isDone();

    V get() throws InterruptedException, ExecutionException;

    V get(long timeout, TimeUnit unit)?

    throws InterruptedException, ExecutionException, TimeoutException;

}

 

cancel Future interface () method is used to cancel execution of the task; isCancelled () method for determining whether a task has been canceled; two get () method will wait for the end of the task execution and obtaining a result, the difference is whether the timeout can be set; Finally isDone () method to determine whether the task has been completed.

While Future demand can be implemented asynchronously get the results, but it does not provide notification mechanism, we do not know when Future completed. In order to obtain a result, we get two or blocked () method returns to wait for the results Future, equivalent to the implementation time synchronization; or use isDone () method to determine Future polling is finished, so that consumes CPU resources. So, Future calls for simple single layer, for nested asynchronous calls is also very heavy, not suitable for complex service link building.

Given the shortcomings of the Future mechanism, Java 8 introduced CompletableFuture mechanism. To make up for the shortcomings of common Future of CompletableFuture to some extent. After the asynchronous task is completed, there is no need to wait for results when we use tasks. Directly through thenAccept (), thenApply (), thenCompose () method such as the results of previous processing asynchronously to another thread to process asynchronous event processing.

CompletableFuture provides a very powerful Future extensions, we can help simplify the complexity of asynchronous programming, and provides the ability to functional programming, the results can be processed by way of correction, conversion and also offers a variety of combinations offered CompletableFuture method.

For routine development work concerned, most of the time we are dealing with a simple task, this time using CompletableFuture really meet the demand. However, when the system is more complex, or we need to deal itself is a very complex task, CompletableFuture for a combination of multiple processes is still not easy. Arrange multiple use CompletableFuture Future is possible, but not easy. We will worry about whether to write out the code really is no problem, but over time, these codes will become increasingly complex and difficult to maintain. To this end, we need to introduce relevant technologies and frameworks reactive programming of these technologies and frameworks to support asynchronous processing easier to maintain the code in the future.

2. mainstream reactive programming techniques to achieve

At present, the mainstream reactive programming implementation techniques include RxJava, Akka Streams, Vert.x and Project Reactor and so on.

(1) RxJava

Reactive Extensions (Rx) is a class library that incorporates asynchronous, based on observable events (Observable) sequence-driven programming, first used in Microsoft's .NET platform. The RxJava is a Java implementation of Reactive Extensions for constructed by using Observable / Flowable serial asynchronous and event-based library, there are two sets of version 1.x and 2.x versions achieved.

RxJava 1.x versions responsive born before the flow specification, although conversion and interfaces responsive stream, but because of the underlying implementation, use is not very intuitive. RxJava 2 consideration in the design and implementation of the integration with existing specifications, in accordance with the flow specification on the interface responsive been rewritten, and the function of the back pressure version 1.x separate out. However, in order to maintain compatibility with RxJava 1.x version, RxJava 2 used in many places it is not intuitive. More details about RxJava may refer to the official website ( http://reactivex.io/ ).

(2) Akka Streams

Akka runs on the JVM, a kit is constructed of high concurrency and high elastic distributed message-driven application. Actor is the core concept Akka, it is an object that encapsulates the state and behavior, can communicate through the exchange of messages between the Actor. By Actor simplifies lock and thread management, you can very easily develop a proper system of parallel and concurrent programs.

Akka is also a member of the initial flow specification responsive, and Akka Akka Streams is based responsive achieve flow, provides an abstraction on top of the higher level of Akka existing role models, support the back pressure responsive mechanism.

(3)Vert.x

Vert.x is an open source Java tool under the Eclipse Foundation, is an asynchronous web application development framework for building highly concurrent, asynchronous, scalable, Web applications, multi-language support. Vert.x is to build responsive systems design, based on event-driven architecture, Vert.x task to achieve a non-blocking mechanism.

Vert.x contains Vert.x Reactive Streams tool magazine, the tool magazine is provided on the realization vert.x responsive flow specification. We can provide flow through Vert.x readable and writable stream processing responsive flow specification of publishers and subscribers.

(4)Project Reactor

Spring 5 introduced reactive programming mechanisms, the default Spring 5 Project Reactor as integrated implementation framework of the mechanism. Reactor birth of late, can be considered a second generation responsive development framework. So it is a fully responsive flow specification based on the design and implementation of the tool library, no RxJava that kind of historical baggage, more intuitive in use. However, the API design and form of expression, Reactor and RxJava compare like with like, we can say Reactor based on responsive flow specification, but in terms of API and as far as possible to move closer to RxJava.

Mono and Flux Reactor core components are the two representative Flux asynchronous sequence comprising 0 to n elements, and comprise asynchronous serial Mono indicates 0 or an element.

 

If you are interested in the article, I can focus on the micro-channel public number: Programmer's transition to an architect.

I published "system architecture design: the transformation of the programmer to architect road", "transition to technology managers: software developers across the industry, technology, transformation of thinking and practice management" and "micro-services architecture and design principles" "micro services architecture combat" and other books, and translated "deep RabbitMQ" and "Spring5 reactive programming combat", welcomed the exchange.

Published 92 original articles · won praise 9 · views 110 000 +

Guess you like

Origin blog.csdn.net/lantian08251/article/details/101520622