File tree Expand file tree Collapse file tree 1 file changed +20
-0
lines changed
hibernate-reactive-core/src/main/java/org/hibernate/reactive/util/impl Expand file tree Collapse file tree 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) {
9090 return CompletableFuture .completedFuture ( value );
9191 }
9292
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+
93113 public static <T > CompletionStage <T > failedFuture (Throwable t ) {
94114 CompletableFuture <T > ret = new CompletableFuture <>();
95115 ret .completeExceptionally ( t );
You can’t perform that action at this time.
0 commit comments