|
1 | 1 | /* |
2 | | - * Copyright (c) 2016-2023 VMware Inc. or its affiliates, All Rights Reserved. |
| 2 | + * Copyright (c) 2016-2024 VMware Inc. or its affiliates, All Rights Reserved. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
119 | 119 | * @author Stephane Maldini |
120 | 120 | * @author David Karnok |
121 | 121 | * @author Simon Baslé |
| 122 | + * @author Injae Kim |
122 | 123 | * |
123 | 124 | * @see Mono |
124 | 125 | */ |
@@ -2132,6 +2133,64 @@ public static <T, D> Flux<T> using(Callable<? extends D> resourceSupplier, Funct |
2132 | 2133 | eager)); |
2133 | 2134 | } |
2134 | 2135 |
|
| 2136 | + /** |
| 2137 | + * Uses an {@link AutoCloseable} resource, generated by a supplier for each individual Subscriber, |
| 2138 | + * while streaming the values from a Publisher derived from the same resource and makes sure |
| 2139 | + * the resource is released if the sequence terminates or the Subscriber cancels. |
| 2140 | + * <p> |
| 2141 | + * Eager {@link AutoCloseable} resource cleanup happens just before the source termination and exceptions raised |
| 2142 | + * by the cleanup Consumer may override the terminal event. |
| 2143 | + * <p> |
| 2144 | + * <img class="marble" src="doc-files/marbles/usingForFlux.svg" alt=""> |
| 2145 | + * <p> |
| 2146 | + * For an asynchronous version of the cleanup, with distinct path for onComplete, onError |
| 2147 | + * and cancel terminations, see {@link #usingWhen(Publisher, Function, Function, BiFunction, Function)}. |
| 2148 | + * |
| 2149 | + * @param resourceSupplier a {@link Callable} that is called on subscribe to generate the resource |
| 2150 | + * @param sourceSupplier a factory to derive a {@link Publisher} from the supplied resource |
| 2151 | + * @param <T> emitted type |
| 2152 | + * @param <D> resource type |
| 2153 | + * |
| 2154 | + * @return a new {@link Flux} built around a disposable resource |
| 2155 | + * @see #usingWhen(Publisher, Function, Function, BiFunction, Function) |
| 2156 | + * @see #usingWhen(Publisher, Function, Function) |
| 2157 | + */ |
| 2158 | + public static <T, D extends AutoCloseable> Flux<T> using(Callable<? extends D> resourceSupplier, |
| 2159 | + Function<? super D, ? extends Publisher<? extends T>> sourceSupplier) { |
| 2160 | + return using(resourceSupplier, sourceSupplier, true); |
| 2161 | + } |
| 2162 | + |
| 2163 | + /** |
| 2164 | + * Uses an {@link AutoCloseable} resource, generated by a supplier for each individual Subscriber, |
| 2165 | + * while streaming the values from a Publisher derived from the same resource and makes sure |
| 2166 | + * the resource is released if the sequence terminates or the Subscriber cancels. |
| 2167 | + * <p> |
| 2168 | + * <ul> |
| 2169 | + * <li>Eager {@link AutoCloseable} resource cleanup happens just before the source termination and exceptions raised |
| 2170 | + * by the cleanup Consumer may override the terminal event.</li> |
| 2171 | + * <li>Non-eager cleanup will drop any exception.</li> |
| 2172 | + * </ul> |
| 2173 | + * <p> |
| 2174 | + * <img class="marble" src="doc-files/marbles/usingForFlux.svg" alt=""> |
| 2175 | + * <p> |
| 2176 | + * For an asynchronous version of the cleanup, with distinct path for onComplete, onError |
| 2177 | + * and cancel terminations, see {@link #usingWhen(Publisher, Function, Function, BiFunction, Function)}. |
| 2178 | + * |
| 2179 | + * @param resourceSupplier a {@link Callable} that is called on subscribe to generate the resource |
| 2180 | + * @param sourceSupplier a factory to derive a {@link Publisher} from the supplied resource |
| 2181 | + * @param eager true to clean before terminating downstream subscribers |
| 2182 | + * @param <T> emitted type |
| 2183 | + * @param <D> resource type |
| 2184 | + * |
| 2185 | + * @return a new {@link Flux} built around a disposable resource |
| 2186 | + * @see #usingWhen(Publisher, Function, Function, BiFunction, Function) |
| 2187 | + * @see #usingWhen(Publisher, Function, Function) |
| 2188 | + */ |
| 2189 | + public static <T, D extends AutoCloseable> Flux<T> using(Callable<? extends D> resourceSupplier, |
| 2190 | + Function<? super D, ? extends Publisher<? extends T>> sourceSupplier, boolean eager) { |
| 2191 | + return using(resourceSupplier, sourceSupplier, Exceptions.AUTO_CLOSE, eager); |
| 2192 | + } |
| 2193 | + |
2135 | 2194 | /** |
2136 | 2195 | * Uses a resource, generated by a {@link Publisher} for each individual {@link Subscriber}, |
2137 | 2196 | * while streaming the values from a {@link Publisher} derived from the same resource. |
|
0 commit comments