Java Flux contains_Reactor:响应式开发库,支持构建基于JVM的非阻塞式应用程序

Reactor Core

68747470733a2f2f6261646765732e6769747465722e696d2f4a6f696e253230436861742e73766768747470733a2f2f6d6176656e2d6261646765732e6865726f6b756170702e636f6d2f6d6176656e2d63656e7472616c2f696f2e70726f6a65637472656163746f722f72656163746f722d636f72652f62616467652e7376673f7374796c653d706c617374696368747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f72656163746f722f72656163746f722d636f72652f616c6c2e737667

68747470733a2f2f6170692e62696e747261792e636f6d2f7061636b616765732f737072696e672f6a6172732f696f2e70726f6a65637472656163746f722f696d616765732f646f776e6c6f61642e737667

68747470733a2f2f7472617669732d63692e6f72672f72656163746f722f72656163746f722d636f72652e7376673f6272616e63683d6d617374657268747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f72656163746f722f72656163746f722d636f72652e73766768747470733a2f2f696d672e736869656c64732e696f2f6c67746d2f67726164652f6a6176612f672f72656163746f722f72656163746f722d636f72652e7376673f6c6f676f3d6c67746d266c6f676f57696474683d313868747470733a2f2f696d672e736869656c64732e696f2f6c67746d2f616c657274732f672f72656163746f722f72656163746f722d636f72652e7376673f6c6f676f3d6c67746d266c6f676f57696474683d3138

Non-Blocking Reactive Streams Foundation for the JVM both implementing a Reactive Extensions inspired API and efficient event streaming support.

The master branch is now dedicated to development of the 3.3.x line.

Getting it

Reactor 3 requires Java 8 or + to run.

With Gradle from repo.spring.io or Maven Central repositories (stable releases only):

repositories {

// maven { url 'https://repo.spring.io/snapshot' }

maven { url 'https://repo.spring.io/milestone' }

mavenCentral()

}

dependencies {

//compile "io.projectreactor:reactor-core:3.4.0-SNAPSHOT"

//testCompile("io.projectreactor:reactor-test:3.4.0-SNAPSHOT")

//TODO change to the release artifact and comment snapshot repo above when GA

compile "io.projectreactor:reactor-core:3.4.0-M1"

testCompile("io.projectreactor:reactor-test:3.4.0-M1")

}

See the reference documentation for more information on getting it (eg. using Maven, or on how to get milestones and snapshots).

Note about Android support: Reactor 3 doesn't officially support nor target Android. However it should work fine with Android SDK 26 (Android O) and above. See the complete note in the reference guide.

Trouble importing the project in IDE?

Since the introduction of Java 9 stubs in order to optimize the performance of debug backtraces, one can sometimes encounter cryptic messages from the build system when importing or re-importing the project in their IDE.

For example:

package StackWalker does not exist: probably building under JDK8 but java9stubs was not added to sources

cannot find symbol @CallerSensitive: probably building with JDK11+ and importing using JDK8

When encountering these issues, one need to ensure that:

Gradle JVM matches the JDK used by the IDE for the modules (in IntelliJ, Modules Settings JDK). Preferably, 1.8.

The IDE is configured to delegate build to Gradle (in IntelliJ: Build Tools > Gradle > Runner and project setting uses that default)

Then rebuild the project and the errors should disappear.

Getting Started

New to Reactive Programming or bored of reading already ? Try the Introduction to Reactor Core hands-on !

If you are familiar with RxJava or if you want to check more detailed introduction, be sure to check https://www.infoq.com/articles/reactor-by-example !

Flux

A Reactive Streams Publisher with basic flow operators.

Static factories on Flux allow for source generation from arbitrary callbacks types.

Instance methods allows operational building, materialized on each subscription (Flux#subscribe(), ...) or multicasting operations (such as Flux#publish and Flux#publishNext).

e29b2399ac914330b8524ce4d3d369c0.png

Flux in action :

Flux.fromIterable(getSomeLongList())

.mergeWith(Flux.interval(100))

.doOnNext(serviceA::someObserver)

.map(d -> d * 2)

.take(3)

.onErrorResume(errorHandler::fallback)

.doAfterTerminate(serviceM::incrementTerminate)

.subscribe(System.out::println);

Mono

A Reactive Streams Publisher constrained to ZERO or ONE element with appropriate operators.

Static factories on Mono allow for deterministic zero or one sequence generation from arbitrary callbacks types.

Instance methods allows operational building, materialized on each Mono#subscribe() or Mono#get() eventually called.

b6ade1eef904fa791f6f8d4b3b399425.png

Mono in action :

Mono.fromCallable(System::currentTimeMillis)

.flatMap(time -> Mono.first(serviceA.findRecent(time), serviceB.findRecent(time)))

.timeout(Duration.ofSeconds(3), errorHandler::fallback)

.doOnSuccess(r -> serviceM.incrementSuccess())

.subscribe(System.out::println);

Blocking Mono result :

Tuple2 nowAndLater =

Mono.zip(

Mono.just(System.currentTimeMillis()),

Flux.just(1).delay(1).map(i -> System.currentTimeMillis()))

.block();

Schedulers

Reactor uses a Scheduler as a contract for arbitrary task execution. It provides some guarantees required by Reactive Streams flows like FIFO execution.

You can use or create efficient schedulers to jump thread on the producing flows (subscribeOn) or receiving flows (publishOn):

Mono.fromCallable( () -> System.currentTimeMillis() )

.repeat()

.publishOn(Schedulers.single())

.log("foo.bar")

.flatMap(time ->

Mono.fromCallable(() -> { Thread.sleep(1000); return time; })

.subscribeOn(Schedulers.parallel())

, 8) //maxConcurrency 8

.subscribe();

ParallelFlux

ParallelFlux can starve your CPU's from any sequence whose work can be subdivided in concurrent tasks. Turn back into a Flux with ParallelFlux#sequential(), an unordered join or use arbitrary merge strategies via 'groups()'.

Mono.fromCallable( () -> System.currentTimeMillis() )

.repeat()

.parallel(8) //parallelism

.runOn(Schedulers.parallel())

.doOnNext( d -> System.out.println("I'm on thread "+Thread.currentThread()) )

.subscribe()

Custom sources : Flux.create and FluxSink, Mono.create and MonoSink

To bridge a Subscriber or Processor into an outside context that is taking care of producing non concurrently, use Flux#create, Mono#create.

Flux.create(sink -> {

ActionListener al = e -> {

sink.next(textField.getText());

};

// without cancellation support:

button.addActionListener(al);

// with cancellation support:

sink.onCancel(() -> {

button.removeListener(al);

});

},

// Overflow (backpressure) handling, default is BUFFER

FluxSink.OverflowStrategy.LATEST)

.timeout(3)

.doOnComplete(() -> System.out.println("completed!"))

.subscribe(System.out::println)

The Backpressure Thing

Most of this cool stuff uses bounded ring buffer implementation under the hood to mitigate signal processing difference between producers and consumers. Now, the operators and processors or any standard reactive stream component working on the sequence will be instructed to flow in when these buffers have free room AND only then. This means that we make sure we both have a deterministic capacity model (bounded buffer) and we never block (request more data on write capacity). Yup, it's not rocket science after all, the boring part is already being worked by us in collaboration with Reactive Streams Commons on going research effort.

What's more in it ?

"Operator Fusion" (flow optimizers), health state observers, helpers to build custom reactive components, bounded queue generator, converters from/to Java 9 Flow, Publisher and Java 8 CompletableFuture. The repository contains a reactor-test project with test features like the StepVerifier.

Reference Guide

Javadoc

Getting started with Flux and Mono

Reactor By Example

Head-First Spring & Reactor

Beyond Reactor Core

Everything to jump outside the JVM with the non-blocking drivers from Reactor Netty.

Reactor Addons provide for adapters and extra operators for Reactor 3.

Sponsored by Pivotal

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值