Interface ProcessingContext
- All Superinterfaces:
ApplicationContext,Context,ProcessingLifecycle
- All Known Implementing Classes:
LegacyMessageSupportingContext,ResourceOverridingProcessingContext
ProcessingLifecycle adding mutable resource management operations by
implementing Context.
It is recommended to construct a Context.ResourceKey instance when adding/updating/removing resources from the
ProcessingContext to allow cross-referral by sharing the key or personalization when the resource should be
private to a specific service.
- Since:
- 5.0.0
- Author:
- Allard Buijze, Gerard Klijs, Milan Savić, Mitchell Herrijgers, Sara Pellegrini, Steven van Beelen
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.axonframework.messaging.core.Context
Context.ResourceKey<T>Nested classes/interfaces inherited from interface org.axonframework.messaging.core.unitofwork.ProcessingLifecycle
ProcessingLifecycle.DefaultPhases, ProcessingLifecycle.ErrorHandler, ProcessingLifecycle.Phase -
Method Summary
Modifier and TypeMethodDescription<T> TcomputeResourceIfAbsent(Context.ResourceKey<T> key, Supplier<T> resourceSupplier) If no resource is present for the givenkey, the givenresourceSupplieris used to supply the instance to register under thiskey.<T> TputResource(Context.ResourceKey<T> key, T resource) Register the givenresourceunder the givenkey.<T> TputResourceIfAbsent(Context.ResourceKey<T> key, T resource) Register the giveninstanceunder the givenkeyif no value is currently present.<T> TremoveResource(Context.ResourceKey<T> key) Removes the resource registered under givenkey.<T> booleanremoveResource(Context.ResourceKey<T> key, T expectedResource) Remove the resource associated with givenkeyif the givenexpectedResourceis the currently associated value.<T> TupdateResource(Context.ResourceKey<T> key, UnaryOperator<@Nullable T> resourceUpdater) Update the resource with givenkeyusing the givenresourceUpdaterto describe the update.default <T> ProcessingContextwithResource(Context.ResourceKey<T> key, T resource) Methods inherited from interface org.axonframework.messaging.core.ApplicationContext
component, componentMethods inherited from interface org.axonframework.messaging.core.Context
containsResource, getResource, resourcesMethods inherited from interface org.axonframework.messaging.core.unitofwork.ProcessingLifecycle
doFinally, isCommitted, isCompleted, isError, isStarted, on, onAfterCommit, onCommit, onError, onInvocation, onPostInvocation, onPreInvocation, onPrepareCommit, runOn, runOnAfterCommit, runOnCommit, runOnInvocation, runOnPostInvocation, runOnPreInvocation, runOnPrepareCommit, whenComplete
-
Method Details
-
withResource
Constructs a newProcessingContext, branching off fromthisProcessingContext.The given
resourceas added to the branchedProcessingContextunder the givenkey.- Specified by:
withResourcein interfaceContext- Type Parameters:
T- The type of resource associated with thekey.- Parameters:
key- The key under which to register theresourcein the branchedProcessingContext.resource- The resource to register in the branchedProcessingContext.- Returns:
- A new
ProcessingContext, branched off fromthisProcessingContext.
-
putResource
Register the givenresourceunder the givenkey.- Type Parameters:
T- The type ofresourceto register under given @code.- Parameters:
key- The key under which to register theresource.resource- The resource to register.- Returns:
- The previously registered
resource, ornullif none was present.
-
updateResource
Update the resource with givenkeyusing the givenresourceUpdaterto describe the update. If no resource is registered with the givenkey, theresourceUpdateris invoked withnull. Otherwise, the function is called with the currently registered resource under that key.The resource is replaced with the return value of the function, or removed when the function returns
null.If the function throws an exception, the exception is rethrown to the caller.
- Type Parameters:
T- The type of resource to update.- Parameters:
key- The key to update the resource for.resourceUpdater- The function performing the update itself.- Returns:
- The new value associated with the
key, ornullwhen removed.
-
putResourceIfAbsent
Register the giveninstanceunder the givenkeyif no value is currently present.- Type Parameters:
T- The type ofresourceto register under givenkey.- Parameters:
key- The key under which to register the resource.resource- The resource to register when nothing is present for the givenkey.- Returns:
- The resource previously associated with given
key.
-
computeResourceIfAbsent
If no resource is present for the givenkey, the givenresourceSupplieris used to supply the instance to register under thiskey.The
resourceSupplierMUST NOT callcomputeResourceIfAbsent(ResourceKey, Supplier)orputResourceIfAbsent(ResourceKey, Object)on thisProcessingContext. The backing resource store rejects re-entrant structural modification and will throwIllegalStateException(surfacing as a "Recursive update"). This matters when stacking decorators that each cache their wrapped instance perProcessingContext: resolve the dependency on the delegate before entering the supplier, rather than from within it.Warning: never use this method for a resource whose construction closes over (holds a reference to) this
ProcessingContextitself - construct a fresh instance directly on every call instead - unlesskeyis guaranteed to be one of the resources every possible branch of this context overrides. A "branch" here is anyProcessingContextreturned bywithResource(ResourceKey, Object): aResourceOverridingProcessingContextthat overrides one specific resource key on top of a shared parent. Such a branch only interceptscomputeResourceIfAbsentfor its own overridden key; every other key falls through to the shared parent, ultimately the root context. If the supplied instance holds ontocontext, andcontextmay be one of several sibling branches of a shared parent (for example, one branch per message being handled within a shared batch), the first branch to call this method gets its instance cached on the shared root, and every sibling branch that calls afterward receives that same stale instance back - silently operating against the wrong branch. For example, this is unsafe:
If// UNSAFE: MyContextAwareGateway's constructor stores a reference to "context". static MyContextAwareGateway forContext(ProcessingContext context) { return context.computeResourceIfAbsent(RESOURCE_KEY, () -> new MyContextAwareGateway(context)); }contextis a per-message branch of a batch, the second message to callforContextreceives the first message's gateway back, closed over the first message's branch. Supply a fresh instance directly instead, bypassing this resource store entirely:// SAFE: always supplies a fresh instance bound to whichever context is passed in. static MyContextAwareGateway forContext(ProcessingContext context) { return new MyContextAwareGateway(context); }This method remains the correct choice when the cached value does not reference
contextand is genuinely meant to be shared for the whole processing session, regardless of how many branches exist. For example:// SAFE: the cached ConcurrentHashMap never references "context", and is meant to be // shared across every branch of the same processing session. var managedEntities = context.computeResourceIfAbsent(managedEntitiesKey, ConcurrentHashMap::new);- Type Parameters:
T- The type of resource registered under givenkey.- Parameters:
key- The key to register the resource for.resourceSupplier- The function to supply the resource to register. Must not call back into the resource store of thisProcessingContext.- Returns:
- The resource associated with the
key.
-
removeResource
Removes the resource registered under givenkey.- Type Parameters:
T- The type of resource associated with thekey.- Parameters:
key- The key to remove the registered resource for.- Returns:
- The value previously associated with the
key.
-
removeResource
Remove the resource associated with givenkeyif the givenexpectedResourceis the currently associated value.- Type Parameters:
T- The type of resource associated with thekey.- Parameters:
key- The key to remove the registered resource for.expectedResource- The expected resource to remove.- Returns:
trueif the resource has been removed, otherwisefalse.
-