{"id":10006291,"date":"2022-08-23T22:08:25","date_gmt":"2022-08-23T14:08:25","guid":{"rendered":"https:\/\/ityet.com\/?p=10006291"},"modified":"2022-08-23T22:08:25","modified_gmt":"2022-08-23T14:08:25","slug":"reactor-%e4%b9%8b-subcribeon-vs-publishon","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006291","title":{"rendered":"Reactor \u4e4b subcribeOn vs publishOn"},"content":{"rendered":"<p>\u6211\u4eec\u4f7f\u7528 subscribeOn \u548c publishOn \u64cd\u4f5c\u7b26\u5728\u54cd\u5e94\u94fe\u4e2d\u5207\u6362\u6267\u884c\u4e0a\u4e0b\u6587(Reactor \u4e2d\u53eb Scheduler)\u3002<\/p>\n<p>\u4e0a\u4e00\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u8bf4\u5230 Reactor \u9ed8\u8ba4\u884c\u4e3a\u662f\u6267\u884c\u8ba2\u9605\u7684\u540c\u4e00\u7ebf\u7a0b\u5c06\u7528\u4e8e\u6574\u4e2a\u7ba1\u9053\u6267\u884c\u3002\u5982\u679c\u8981\u5207\u6362\u6267\u884c\u7ebf\u7a0b\u600e\u4e48\u529e\uff1f\u53ef\u4ee5\u4f7f\u7528 publishOn \u548c SubscribeOn<\/p>\n<p>\u8ba9\u6211\u4eec\u770b\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code>class ReactiveJavaTutorial {\n\n  public static void main(String[] args) {\n\n    Flux&lt;String&gt; cities = Flux.just(&quot;New York&quot;, &quot;London&quot;, &quot;Paris&quot;, &quot;Amsterdam&quot;)\n            .map(String::toUpperCase)\n            .filter(cityName -&gt; cityName.length() &lt;= 8)\n            .map(cityName -&gt; cityName.concat(&quot; City&quot;))\n            .log();\n\n    cities.subscribe();\n\n  }<\/code><\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre><code>17:39:41.693 [main] DEBUG reactor.util.Loggers - Using Slf4j logging framework\n17:39:41.712 [main] INFO reactor.Flux.MapFuseable.1 - | onSubscribe([Fuseable] FluxMapFuseable.MapFuseableSubscriber)\n17:39:41.714 [main] INFO reactor.Flux.MapFuseable.1 - | request(unbounded)\n17:39:41.715 [main] INFO reactor.Flux.MapFuseable.1 - | onNext(NEW YORK City)\n17:39:41.715 [main] INFO reactor.Flux.MapFuseable.1 - | onNext(LONDON City)\n17:39:41.715 [main] INFO reactor.Flux.MapFuseable.1 - | onNext(PARIS City)\n17:39:41.716 [main] INFO reactor.Flux.MapFuseable.1 - | onComplete()<\/code><\/pre>\n<p>\u4e2d\u62ec\u53f7\u4e2d\u7684\u5c31\u662f\u7ebf\u7a0b\u540d\u79f0\uff0c\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u90fd\u662f <strong>main<\/strong>\u3002\u53ef\u4ee5\u770b\u5230\u6574\u4e2a\u7ba1\u9053\u6267\u884c\u5668\u4e2d\u90fd\u662f\u4f7f\u7528\u7684 main \u7ebf\u7a0b\u3002<\/p>\n<p>\u6709\u7684\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u60f3\u544a\u8bc9 Reactor \u522b\u5728\u6574\u4e2a\u7ba1\u9053\u4e2d\u4f7f\u7528\u540c\u4e00\u4e2a\u7ebf\u7a0b\u3002\u6211\u53ef\u4ee5\u4f7f\u7528 subscribeOn() \u548c publishOn() \u65b9\u6cd5\u8fbe\u5230\u6548\u679c\u3002<\/p>\n<h2>subscribeOn() \u65b9\u6cd5<\/h2>\n<p><code>subscribeOn()<\/code> \u65b9\u6cd5\u9002\u7528\u4e8e\u8ba2\u9605\u8fc7\u7a0b\u3002<strong>\u6211\u4eec\u53ef\u4ee5\u628a\u5b83\u653e\u5728\u54cd\u5e94\u94fe\u6761\u4e2d\u7684\u4efb\u610f\u4f4d\u7f6e<\/strong>\u3002\u5b83\u63a5\u6536 Scheduler \u53c2\u6570\uff0c\u4e14\u5728\u63d0\u4f9b\u7684\u7ebf\u7a0b\u6c60\u4e2d\u9009\u62e9\u7ebf\u7a0b\u6267\u884c\u3002<\/p>\n<p>\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6709\u754c\u5f39\u6027\u7ebf\u7a0b\u6c60<code>\uff08Schedulers.boundElastic()\uff09<\/code>\u3002<\/p>\n<pre><code>@Test\npublic void testSubscribeThread() {\n  Flux&lt;String&gt; cities = Flux.just(&quot;New York&quot;, &quot;London&quot;, &quot;Paris&quot;, &quot;Amsterdam&quot;)\n      .subscribeOn(Schedulers.boundedElastic())\n      .map(String::toUpperCase)\n      .filter(cityName -&gt; cityName.length() &lt;= 8)\n      .map(cityName -&gt; cityName.concat(&quot; City&quot;))\n      .map(TestCase::concat)\n      .map(TestCase::stringToUpperCase)\n      .log();\n\n\/\/        cities.subscribe();\n  System.out.println(cities.blockFirst());\n}<\/code><\/pre>\n<p>PS: \u539f\u6587\u63d0\u4f9b\u7684case\uff0c\u6ca1\u6709\u8f93\u51fa\uff0c\u7b80\u5355\u4fee\u6539\u4e86\u4e00\u4e0b\u3002<\/p>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre><code>20:07:53.517 [main] DEBUG reactor.util.Loggers - Using Slf4j logging framework\n20:07:53.558 [main] INFO reactor.Flux.Map.1 - onSubscribe(FluxMap.MapSubscriber)\n20:07:53.560 [main] INFO reactor.Flux.Map.1 - request(unbounded)\nconcat: boundedElastic-1\nstringToUpperCase: boundedElastic-1\n20:07:53.564 [boundedElastic-1] INFO reactor.Flux.Map.1 - onNext(NEW YORK CITY CITY)\n20:07:53.565 [boundedElastic-1] INFO reactor.Flux.Map.1 - cancel()\nNEW YORK CITY CITY<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230 <code>main<\/code> \u7ebf\u7a0b\u5f00\u59cb\u8ba2\u9605\uff0c\u4f46\u662f\u88ab\u5207\u6362\u6210 <code>boundedElastic-1<\/code> \u7ebf\u7a0b\u3002\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u4e2a <code>Scheduler \uff08Schedulers.boundedElastic()\uff09<\/code>\uff0c\u7136\u540e\u8fd9\u4e2a\u7ebf\u7a0b\u6c60\u4e2d\u7684\u4e00\u4e2a\u7ebf\u7a0b\u88ab\u9009\u4e2d\u6765\u66ff\u6362 <code>main<\/code> \u7ebf\u7a0b\u3002<\/p>\n<h2>publishOn() \u65b9\u6cd5<\/h2>\n<p><code>publishOn()<\/code> \u65b9\u6cd5\u8ddf <code>subscribeOn()<\/code> \u5f88\u7c7b\u4f3c\uff0c\u4f46\u662f\u6709\u4e00\u4e2a\u4e3b\u8981\u533a\u522b\u3002<\/p>\n<p>\u6765\u770b\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre><code>class ReactiveJavaTutorial {\n\n  public static void main(String[] args) {\n\n    Flux.just(&quot;New York&quot;, &quot;London&quot;, &quot;Paris&quot;, &quot;Amsterdam&quot;)\n            .map(ReactiveJavaTutorial::stringToUpperCase)\n            .publishOn(Schedulers.boundedElastic())\n            .map(ReactiveJavaTutorial::concat)\n            .subscribe();\n  }\n\n  private static String stringToUpperCase(String name) {\n    System.out.println(&quot;stringToUpperCase: &quot; + Thread.currentThread().getName());\n    return name.toUpperCase();\n  }\n\n  private static String concat(String name) {\n    System.out.println(&quot;concat: &quot; + Thread.currentThread().getName());\n    return name.concat(&quot; City&quot;);\n  }\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\uff0c\u6211\u4eec\u5728\u4e24\u4e2a map \u64cd\u4f5c\u4e2d\u653e\u4e00\u4e2a <code>publishOn()<\/code>\u3002\u6211\u4eec\u6765\u770b\u8f93\u51fa\uff1a<\/p>\n<pre><code>stringToUpperCase: main\nstringToUpperCase: main\nstringToUpperCase: main\nconcat: boundedElastic-1\nconcat: boundedElastic-1\nconcat: boundedElastic-1<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u6240\u6709\u5728 <code>publishOn<\/code> \u64cd\u4f5c\u4e4b\u524d\u7684\u90fd\u662f main \u7ebf\u7a0b\u6267\u884c\uff0c\u6240\u6709 <code>publishOn<\/code> \u4e4b\u540e\u7684\u90fd\u662f boundedElastic-1 \u6267\u884c\u3002\u8fd9\u662f\u56e0\u4e3a <code>publishOn<\/code> \u5145\u5f53\u4efb\u4f55\u5176\u4ed6\u64cd\u4f5c\u7b26\u3002\u5b83\u4ece\u4e0a\u6e38\u63a5\u6536\u4fe1\u53f7\uff0c\u5e76\u5728\u5173\u8054\u7684 <code>Scheduler<\/code> \u4e0a\u5bf9\u4e00\u4e2a <code>worker<\/code> \u6267\u884c\u56de\u8c03\u65f6\u5411\u4e0b\u6e38\u91cd\u64ad\u3002<\/p>\n<p>\u8fd9\u5c31\u662f <code>publishOn<\/code> \u548c <code>subscribeOn()<\/code> \u7684\u4e3b\u8981\u533a\u522b\u3002\u65e0\u8bba\u6211\u4eec\u628a <code>subscribeOn()<\/code> \u653e\u5728\u54ea\u91cc\uff0c\u5b83\u63d0\u4f9b\u7684 <code>Scheduler<\/code> \u90fd\u4f1a\u5e94\u7528\u5230\u6574\u6761\u54cd\u5e94\u94fe\u3002<\/p>\n<p><a href=\"https:\/\/link.segmentfault.com\/?enc=I6hGSNuxTgrkpfiNdLYSUQ%3D%3D.isBhqfR7iI3TT6cbLZRXwyY5iXfcbmj0SEBrADJIrZVFdq2hjNol1FG%2BQYWpZ%2BqAei%2BJ0xLaE7DAhJlx5wf7HQHArK20b9dXEol3d8klfyi8TPcXY7QZnmPQw0UXapKh\" rel=\"nofollow\">subscribeOn and publishOn operators in Project Reactor<\/a><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042365807\">https:\/\/segmentfault.com\/a\/1190000042365807<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u4f7f\u7528 subscribeOn \u548c publishOn \u64cd\u4f5c\u7b26\u5728\u54cd\u5e94\u94fe\u4e2d\u5207\u6362\u6267\u884c\u4e0a\u4e0b\u6587(Reactor \u4e2d\u53eb [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006291","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006291"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006291\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}