Class MaterializingResource<T>

java.lang.Object
org.axonframework.common.tx.MaterializingResource<T>
Type Parameters:
T - the type of the resource

@Internal public class MaterializingResource<T> extends Object
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 Details

    • MaterializingResource

      public MaterializingResource(Callable<T> resourceSupplier)
      Constructs a new instance.
      Parameters:
      resourceSupplier - supplies the resource to use, invoked at most once, cannot be null
      Throws:
      NullPointerException - if resourceSupplier is null
  • Method Details

    • withMaterializationShielded

      public <R> R withMaterializationShielded(Function<Boolean,R> decision)
      Runs decision, passing whether this holder already has a resource materialized, while preventing a concurrent apply(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 by decision
      Parameters:
      decision - given true if this holder already has a resource materialized, otherwise false; run shielded from a concurrent materialization, cannot be null
      Returns:
      the result of decision
      Throws:
      NullPointerException - when decision is null
    • apply

      public <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.
      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 type R, cannot be null
      Returns:
      a CompletableFuture which when it completes contains the result of function, never null
      Throws:
      NullPointerException - when function is null