Spring Reactor 项目核心库
Reactor Core
Non-Blocking Reactive Streams Foundation for the JVM both implementing a Reactive Extensions inspired API and efficient event streaming support.
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 'http://repo.spring.io/snapshot' } maven { url 'http://repo.spring.io/milestone' } mavenCentral() } dependencies { //compile "io.projectreactor:reactor-core:3.1.4.RELEASE" //testCompile("io.projectreactor:reactor-test:3.1.4.RELEASE") compile "io.projectreactor:reactor-core:3.2.0.M1" testCompile("io.projectreactor:reactor-test:3.2.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.
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 detailled 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 Flux#subscribe(), Flux#subscribe() or multicasting operations such as Flux#publish and Flux#publishNext.
Flux in action :
Flux.fromIterable(getSomeLongList()) .mergeWith(Flux.interval(100)) .doOnNext(serviceA::someObserver) .map(d -> d * 2) .take(3) .onErrorResumeWith(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.
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<Long, Long> 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 abitrary 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, hash-wheel timer, 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
http://projectreactor.io/docs/core/release/reference/docs/index.html
Javadoc
https://projectreactor.io/docs/core/release/api/
Getting started with Flux and Mono
https://github.com/reactor/lite-rx-api-hands-on
Reactor By Example
https://www.infoq.com/articles/reactor-by-example
Head-First Spring & Reactor
https://github.com/reactor/head-first-reactive-with-spring-and-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.
Powered by Reactive Streams Commons
Licensed under Apache Software License 2.0
Sponsored by Pivotal
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Android WebView加载网页,图片等元素宽度大小不兼容手机屏幕的宽度
无论是Android手机还是iOS手机,使用WebView加载一些没有专门为手机屏幕做适配的网页的时候,就会出现网页中的图片等标签元素的宽度大小和手机屏幕的宽度不一致的情况,如下图 之所以会出现上面的情况,是因为网页中的dom元素大小没有适配到移动网页中,有些元素是把宽度固定死的 解决办法有很多: 前端人员将网页适配到移动端(优点:最为规范的做法 缺点:一些不再维护的项目,是没有前端人员来适配) 移动端开发人员自己通过JS的注入来修改(优点:在缺乏前端人员的支持下,可以勉强这样做 缺点:元素变化的话,Native需要修改各种JS函数) 现在重点就说下第二种方法: 首先在Android和iOS这两个平台,WebView可以和Native进行交互的,在Native中编写JS函数注入到html中,也可以在网页调用Native的函数。 这里我就简单讲一下通过JS注入到html中,来改变相应元素的大小,以及字体等,Android和iOS都有WebView,都可以监听监听网页的加载的过程,在Android中通过WebViewClient来监听网页的加载过程,一般我们需要在网页所有资源加...
- 下一篇
威锋网金狮奖落下大幕,2017年度科技榜单出炉
期待2018! 2017年度威锋网金狮奖在昨天关闭了投票通道,短短一个月时间转瞬即逝,在前半个月的征集阶段中,网友们纷纷提交了自己喜爱的数码产品和iOS APP,而在后半个月的投票阶段中大家也是见证了好几对“冤家”之间的激烈竞争。不过现在一切都圆满落幕了,在统计完超过15万张选票之后,各大板块的获奖名单纷纷出炉,是不是迫不及待地想看到最后的结果了?Let's go! 科技硬件类——实用和颜值才是王道 在最受关注的智能手机排名中,iPhone X毫无意外地成为最受欢迎的产品,9222张选票远远超过其他对手,不过令人意外的是,三星Galaxy S8竟然击败了下半年发布的Note 8、华为Mate 10和小米MIX 2等产品,看来比起双摄等配置,高颜值才是大家最关注的卖点啊!而小米MIX 2作为现象级的全面屏手机,也夺得了第三名。 而在影音娱乐提名中,1MORE三单元圈铁耳机作为今年现象级的传统有线耳机,也一举击败了热度超高的AirPods和任天堂Switch和BOSE QC35等产品,看来荣获iF设计大奖和美国《消费者报告》“2017年夏季最佳耳机”奖项确实为它增色不少,也从侧面证实了现在...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Hadoop3单机部署,实现最简伪集群
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- CentOS8编译安装MySQL8.0.19
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- SpringBoot2全家桶,快速入门学习开发网站教程
- CentOS7,CentOS8安装Elasticsearch6.8.6