1
1
# <div align =" center " ><a href =" #dummy " ><img src =" https://github.com/foldright/cffu/assets/1063891/124658cd-025f-471e-8da1-7eea0e482915 " alt =" 🦝 CompletableFuture-Fu(CF-Fu) " ></a ></div >
2
2
3
3
<p align =" center " >
4
- <a href =" https://github.com/foldright/cffu/actions/workflows/fast_ci.yaml " ><img src =" https://img.shields.io/github/actions/workflow/status/foldright/cffu/fast_ci.yaml?branch=main&logo=github&logoColor=white&label=fast ci " alt =" Fast CI - GH Workflow Build Status " ></a >
5
- <a href =" https://github.com/foldright/cffu/actions/workflows/ci.yaml " ><img src =" https://img.shields.io/github/actions/workflow/status/foldright/cffu/ci.yaml?branch=main&logo=github&logoColor=white&label=strong ci " alt =" Strong CI - GH Workflow Build Status " ></a >
4
+ <a href =" https://github.com/foldright/cffu/actions/workflows/fast_ci.yaml " ><img src =" https://img.shields.io/github/actions/workflow/status/foldright/cffu/fast_ci.yaml?branch=main&logo=github&logoColor=white&label=fast%20ci " alt =" Fast Build CI " ></a >
5
+ <a href =" https://github.com/foldright/cffu/actions/workflows/ci.yaml " ><img src =" https://img.shields.io/github/actions/workflow/status/foldright/cffu/ci.yaml?branch=main&logo=github&logoColor=white&label=strong%20ci " alt =" Strong Build CI " ></a >
6
6
<a href =" https://app.codecov.io/gh/foldright/cffu/tree/main " ><img src =" https://img.shields.io/codecov/c/github/foldright/cffu/main?logo=codecov&logoColor=white " alt =" Codecov " ></a >
7
- <a href =" https://qodana.cloud/projects/A61Yy " ><img src =" https://img.shields.io/github/actions/workflow/status/foldright/cffu/qodana_code_quality.yml?branch=main&logo=jetbrains&logoColor=white&label=qodana " alt =" Qodana - GH Workflow Build Status " ></a >
7
+ <a href =" https://qodana.cloud/projects/A61Yy " ><img src =" https://img.shields.io/github/actions/workflow/status/foldright/cffu/qodana_code_quality.yml?branch=main&logo=jetbrains&logoColor=white&label=qodana " alt =" Qodana Code Inspections " ></a >
8
8
<a href =" https://openjdk.java.net/ " ><img src =" https://img.shields.io/badge/Java-8+-339933?logo=openjdk&logoColor=white " alt =" Java support " ></a >
9
9
<a href =" https://www.apache.org/licenses/LICENSE-2.0.html " ><img src =" https://img.shields.io/github/license/foldright/cffu?color=4D7A97&logo=apache " alt =" License " ></a >
10
10
<a href =" https://foldright.io/api-docs/cffu/ " ><img src =" https://img.shields.io/github/release/foldright/cffu?label=javadoc&color=339933&logo=read-the-docs&logoColor=white " alt =" Javadocs " ></a >
@@ -472,31 +472,52 @@ public class ConcurrencyStrategyDemo {
472
472
473
473
### 2.4 支持直接运行多个` Action ` ,而不是要先包装成` CompletableFuture `
474
474
475
- ` CompletableFuture ` 的` allOf/anyOf ` 方法输入的是` CompletableFuture ` ,当业务直接有要编排业务逻辑方法时仍然需要先包装成 ` CompletableFuture ` 再运行:
475
+ ` CompletableFuture ` 的` allOf/anyOf ` 方法输入的是` CompletableFuture ` ;当业务直接有要编排业务逻辑方法时,仍然需要先包装成 ` CompletableFuture ` 再运行:
476
476
477
477
- 繁琐
478
- - 也模糊了业务流程
478
+ - 模糊了业务流程
479
+ - 简单包装多个` Action ` 成` CF ` 提交给` allOf/anyOf ` 的做法(业务代码往/往是这样实现的),** 会呑异常**
480
+ - 当输入` Action ` 的运行抛出多个异常时,这些异常至多只能有一个能通过返回` CF ` 反馈给业务,其它的异常则被默默地呑掉,影响业务问题的排查
479
481
480
- ` cffu ` 提供了直接运行多个` Action ` 的方法,方便直接明了地表达业务编排流程。
482
+ ` cffu ` 提供了直接运行多个` Action ` 的方法,解决上述问题:
483
+
484
+ - 方便直接明了地表达与编排业务流程
485
+ - 不呑异常,方便排查业务问题
486
+ - 当多个输入` Action ` 的运行抛出多个异常时,会打印日志报告出没有在返回` CF ` 中反馈给业务的异常
481
487
482
488
示例代码如下:
483
489
484
490
``` java
485
491
public class MultipleActionsDemo {
492
+ private static final ExecutorService myBizExecutor = Executors . newCachedThreadPool();
493
+ private static final CffuFactory cffuFactory = CffuFactory . builder(myBizExecutor). build();
494
+
486
495
static void mRunAsyncDemo () {
487
- // wrap tasks to CompletableFuture first, AWKWARD! 😖
496
+ // wrap actions to CompletableFutures first, AWKWARD! 😖
488
497
CompletableFuture . allOf(
489
498
CompletableFuture . runAsync(() - > System . out. println(" task1" )),
490
499
CompletableFuture . runAsync(() - > System . out. println(" task2" )),
491
500
CompletableFuture . runAsync(() - > System . out. println(" task3" ))
492
501
);
502
+ completedFuture(" task" ). thenCompose(v - >
503
+ CompletableFuture . allOf(
504
+ CompletableFuture . runAsync(() - > System . out. println(v + " 1" )),
505
+ CompletableFuture . runAsync(() - > System . out. println(v + " 2" )),
506
+ CompletableFuture . runAsync(() - > System . out. println(v + " 3" ))
507
+ )
508
+ );
493
509
494
510
// just run multiple actions, fresh and cool 😋
495
511
CompletableFutureUtils . mRunAsync(
496
512
() - > System . out. println(" task1" ),
497
513
() - > System . out. println(" task2" ),
498
514
() - > System . out. println(" task3" )
499
515
);
516
+ cffuFactory. completedFuture(" task" ). thenMAcceptAsync(
517
+ (String v) - > System . out. println(v + " 1" ),
518
+ v - > System . out. println(v + " 2" ),
519
+ v - > System . out. println(v + " 3" )
520
+ );
500
521
}
501
522
}
502
523
```
@@ -511,7 +532,7 @@ public class MultipleActionsDemo {
511
532
private static final CffuFactory cffuFactory = CffuFactory . builder(myBizExecutor). build();
512
533
513
534
static void thenMApplyAsyncDemo () {
514
- // wrap tasks to CompletableFuture first, AWKWARD! 😖
535
+ // wrap actions to CompletableFutures first, AWKWARD! 😖
515
536
completedFuture(42 ). thenCompose(v - >
516
537
CompletableFutureUtils . allResultsFailFastOf(
517
538
CompletableFuture . supplyAsync(() - > v + 1 ),
@@ -521,7 +542,8 @@ public class MultipleActionsDemo {
521
542
). thenAccept(System . out:: println);
522
543
// output: [43, 44, 45]
523
544
cffuFactory. completedFuture(42 ). thenCompose(v - >
524
- CompletableFutureUtils . allResultsFailFastOf(
545
+ CompletableFutureUtils . allSuccessResultsOf(
546
+ - 1 ,
525
547
CompletableFuture . supplyAsync(() - > v + 1 ),
526
548
CompletableFuture . supplyAsync(() - > v + 2 ),
527
549
CompletableFuture . supplyAsync(() - > v + 3 )
@@ -537,14 +559,15 @@ public class MultipleActionsDemo {
537
559
v - > v + 3
538
560
). thenAccept(System . out:: println);
539
561
// output: [43, 44, 45]
540
- cffuFactory. completedFuture(42 ). thenMApplyFailFastAsync(
562
+ cffuFactory. completedFuture(42 ). thenMApplyAllSuccessAsync(
563
+ - 1 ,
541
564
v - > v + 1 ,
542
565
v - > v + 2 ,
543
566
v - > v + 3
544
567
). thenAccept(System . out:: println);
545
568
// output: [43, 44, 45]
546
569
547
- CompletableFutureUtils . thenMApplyAllSuccessTupleAsync (
570
+ CompletableFutureUtils . thenMApplyTupleFailFastAsync (
548
571
completedFuture(42 ),
549
572
v - > " string" + v,
550
573
v - > v + 1 ,
0 commit comments