Package org.axonframework.common.tx
Class MaterializingResource<T>
java.lang.Object
org.axonframework.common.tx.MaterializingResource<T>
- Type Parameters:
T- the type of the resource
Lazily obtains a resource from the given
resourceSupplier on first use, caches it, and reuses it
for every subsequent apply(ThrowingFunction) call.
All calls are serialized against each other, since the cached resource is generally not safe for
concurrent use. withMaterializationShielded(Function) additionally guards materialization
against a concurrent caller preferring an independent resource while this one isn't active yet.
- Since:
- 5.4.0
- Author:
- John Hendrikx
-
Constructor Summary
ConstructorsConstructorDescriptionMaterializingResource(Callable<T> resourceSupplier) Constructs a new instance. -
Method Summary
Modifier and TypeMethodDescription<R> CompletableFuture<R> apply(ThrowingFunction<T, R, Exception> function) Executes a transactional operation that returns a result, materializing the resource first if not already done.<R> RwithMaterializationShielded(Function<Boolean, R> decision) Runsdecision, passing whether this holder already has a resource materialized, while preventing a concurrentapply(org.axonframework.common.function.ThrowingFunction<T, R, java.lang.Exception>)call from materializing it for the duration - so a caller can fall back to an independent executor without racing a concurrent materialization.
-
Constructor Details
-
MaterializingResource
Constructs a new instance.- Parameters:
resourceSupplier- supplies the resource to use, invoked at most once, cannot benull- Throws:
NullPointerException- ifresourceSupplierisnull
-
-
Method Details
-
withMaterializationShielded
Runsdecision, passing whether this holder already has a resource materialized, while preventing a concurrentapply(org.axonframework.common.function.ThrowingFunction<T, R, java.lang.Exception>)call from materializing it for the duration - so a caller can fall back to an independent executor without racing a concurrent materialization.- Type Parameters:
R- the type of the result produced bydecision- Parameters:
decision- giventrueif this holder already has a resource materialized, otherwisefalse; run shielded from a concurrent materialization, cannot benull- Returns:
- the result of
decision - Throws:
NullPointerException- whendecisionisnull
-
apply
Executes a transactional operation that returns a result, materializing the resource first if not already done.- Type Parameters:
R- the type of the result returned by the function- Parameters:
function- a function that accepts the resource and produces a result of typeR, cannot benull- Returns:
- a
CompletableFuturewhich when it completes contains the result offunction, nevernull - Throws:
NullPointerException- whenfunctionisnull
-