File tree 1 file changed +20
-0
lines changed
hibernate-reactive-core/src/main/java/org/hibernate/reactive/util/impl
1 file changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -90,6 +90,26 @@ public static <T> CompletionStage<T> completedFuture(T value) {
90
90
return CompletableFuture .completedFuture ( value );
91
91
}
92
92
93
+ /**
94
+ * Useful for implementing try-finally blocks.
95
+ * For example:
96
+ * <pre>{@code
97
+ * return supplyStage( () -> {
98
+ * // Any error here will get caught
99
+ * ...
100
+ * })
101
+ * .whenComplete( (r,t) -> {
102
+ * // finally block
103
+ * ...
104
+ * })
105
+ * }</pre>
106
+ */
107
+ public static <T > CompletionStage <T > supplyStage (Supplier <CompletionStage <T >> supplier ) {
108
+ // Using the voidFuture() is the simplest way I found to make sure that everything run in the correct executor
109
+ return voidFuture ()
110
+ .thenCompose ( v -> supplier .get () );
111
+ }
112
+
93
113
public static <T > CompletionStage <T > failedFuture (Throwable t ) {
94
114
CompletableFuture <T > ret = new CompletableFuture <>();
95
115
ret .completeExceptionally ( t );
You can’t perform that action at this time.
0 commit comments