Reactive - 12 - Blocking to Reactive

翻译自:https://tech.io/playgrounds/929/reactive-programming-with-reactor-3/BlockingToReactive

Blocking to Reactive


Description


The big question is "How to deal with legacy, non reactive code?".

最大的问题是“如何处理遗留的、非反应性的代码?”。

Say you have blocking code (eg. a JDBC connection to a database), and you
want to integrate that into your reactive pipelines while avoiding too much
of an impact on performance.

假设您有阻塞代码(例如,到数据库的JDBC连接),您希望将其集成到反应式管道中,同时避免对性能造成太多影响。

The best course of action is to isolate such intrinsically blocking parts
of your code into their own execution context via a Scheduler, keeping
the efficiency of the rest of the pipeline high and only creating extra
threads when absolutely needed.

最好的做法是通过调度器将代码中这些固有的阻塞部分隔离到它们自己的执行上下文中,
从而保持管道其余部分的高效率,并且仅在绝对需要时创建额外的线程。

In the JDBC example you could use the fromIterable factory method.
But how do you prevent the call to block the rest of the pipeline?

在JDBC示例中,可以使用fromIterable工厂方法。但是,如何防止调用阻塞管道的其余部分呢?

Practice


The subscribeOn method allow to isolate a sequence from the start on a
provided Scheduler. For example, the Schedulers.boundedElastic() will
create a pool of threads that grows on demand, releasing threads that
haven't been used in a while automatically. In order to avoid too many
threads due to abusing of this easy option, the boundedElastic Scheduler
places an upper limit to the number of threads it can create and reuse
(unlike the now deprecated elastic() one).

subscribeOn方法允许在提供的Scheduler上从开始隔离序列。例如,Schedulers.boundedElastic()
将创建一个按需增长的线程池,自动释放一段时间未使用的线程。为了避免由于滥用此easy选项而导致过多线程,
boundedElastic调度程序对其可以创建和重用的线程数设定了上限(与现在已弃用的elastic()不同)。

Use that trick to slowly read all users from the blocking repository
in the first exercise. Note that you will need to wrap the call to the
repository inside a Flux.defer lambda.

在第一个练习中,使用该技巧慢慢读取阻塞repository中的所有用户。
注意,您需要将对repository的调用封装在Flux.defer lambda中。
    // Create a Flux for reading all users from the blocking repository deferred until the flux is subscribed, and run it with a bounded elastic scheduler
    Flux<User> blockingRepositoryToFlux(BlockingRepository<User> repository) {
        return Flux.defer(() -> Flux.fromIterable(repository.findAll()).subscribeOn(Schedulers.boundedElastic()));
    }

For slow subscribers (eg. saving to a database), you can isolate a smaller
section of the sequence with the publishOn operator. Unlike subscribeOn,
it only affects the part of the chain below it, switching it to a new Scheduler.

对于速度较慢的订阅者(例如保存到数据库),可以使用publishOn运算符隔离序列的较小部分。
与subscribeOn不同,它只影响其下方链的一部分,将其切换到新的Scheduler。

As an example, you can use doOnNext to perform a save on the repository,
but first use the trick above to isolate that save into its own execution
context. You can make it more explicit that you're only interested in knowing
if the save succeeded or failed by chaining the then() operator at the end,
which returns a Mono<Void>.

例如,您可以使用doOnNext在repository上执行保存,但首先使用上述技巧将该保存隔离到其自己的
执行上下文中。通过在末尾链接then()运算符,您可以更明确地知道保存是成功还是失败,这将返回一个Mono<Void>。
    // Insert users contained in the Flux parameter in the blocking repository using a bounded elastic scheduler and return a Mono<Void> that signal the end of the operation
    Mono<Void> fluxToBlockingRepository(Flux<User> flux, BlockingRepository<User> repository) {
        return flux.publishOn(Schedulers.boundedElastic()).doOnNext(repository::save).then();
    }
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,546评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,224评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,911评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,737评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,753评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,598评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,338评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,249评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,696评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,888评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,013评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,731评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,348评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,929评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,048评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,203评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,960评论 2 355

推荐阅读更多精彩内容