Package org.apache.camel.spi
Interface UnitOfWork
public interface UnitOfWork
An object representing the unit of work processing an
Exchange which allows the use of
Synchronization hooks. This object might map one-to-one with a transaction in JPA or Spring; or might not.-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionvoidaddSynchronization(Synchronization synchronization) Adds a synchronization hookvoidafterProcess(Processor processor, Exchange exchange, AsyncCallback callback, boolean doneSync) Strategy for work to be executed after the processingvoidafterRoute(Exchange exchange, Route route) Invoked when this unit of work is done being routed by the given route.beforeProcess(Processor processor, Exchange exchange, AsyncCallback callback) Strategy for work to be executed before processing.voidbeforeRoute(Exchange exchange, Route route) Invoked when this unit of work is about to be routed by the given route.voidbeginTransactedBy(Object key) Mark this UnitOfWork as being transacted by the given transaction key.booleancontainsSynchronization(Synchronization synchronization) Checks if the passed synchronization hook is already part of this unit of work.createChildUnitOfWork(Exchange childExchange) Create a child unit of work, which is associated to this unit of work as its parent.voidInvoked when this unit of work has been completed, whether it has failed or completedvoidendTransactedBy(Object key) Mark this UnitOfWork as not transacted anymore by the given transaction definition.Gets the original INMessagethis Unit of Work was started with.getRoute()Gets theRoutethat thisUnitOfWorkcurrently is being routed through.voidhandoverSynchronization(Exchange target) Handover all the registered synchronizations to the targetExchange.voidhandoverSynchronization(Exchange target, Predicate<Synchronization> filter) Handover all the registered synchronizations to the targetExchange.booleanWhether the unit of work should call the before/after process methods or not.booleanAre we transacted?booleanisTransactedBy(Object key) Are we already transacted by the given transaction key?booleanPrepares this unit of work with the given inputExchangepopRoute()When finished being routed under the currentRouteit should be removed.voidPushes theRoutethat thisUnitOfWorkcurrently is being routed through.voidremoveSynchronization(Synchronization synchronization) Removes a synchronization hookvoidreset()Clears the unit of work from user data, so it may be reused.intGets theRoutelevel-of-depth that thisUnitOfWorkcurrently is being routed through.introuteStackLevel(boolean includeRouteTemplate, boolean includeKamelet) Gets theRoutelevel-of-depth that thisUnitOfWorkcurrently is being routed through.voidsetParentUnitOfWork(UnitOfWork parentUnitOfWork) Sets the parent unit of work.
-
Field Details
-
MDC_BREADCRUMB_ID
- See Also:
-
MDC_EXCHANGE_ID
- See Also:
-
MDC_MESSAGE_ID
- See Also:
-
MDC_CORRELATION_ID
- See Also:
-
MDC_ROUTE_ID
- See Also:
-
MDC_STEP_ID
- See Also:
-
MDC_CAMEL_CONTEXT_ID
- See Also:
-
MDC_TRANSACTION_KEY
- See Also:
-
-
Method Details
-
reset
void reset()Clears the unit of work from user data, so it may be reused. Important: This API is NOT intended for Camel end users, but used internally by Camel itself. -
onPrepare
Prepares this unit of work with the given inputExchange- Parameters:
exchange- the exchange- Returns:
- true if the unit of work was created and prepared, false if already prepared
-
addSynchronization
Adds a synchronization hook- Parameters:
synchronization- the hook
-
removeSynchronization
Removes a synchronization hook- Parameters:
synchronization- the hook
-
containsSynchronization
Checks if the passed synchronization hook is already part of this unit of work.- Parameters:
synchronization- the hook- Returns:
- true, if the passed synchronization is part of this unit of work, else false
-
handoverSynchronization
Handover all the registered synchronizations to the targetExchange. This is used when a route turns into asynchronous and theExchangethat is continued and routed in the async thread should do the on completion callbacks instead of the original synchronous thread.- Parameters:
target- the target exchange
-
handoverSynchronization
Handover all the registered synchronizations to the targetExchange. This is used when a route turns into asynchronous and theExchangethat is continued and routed in the async thread should do the on completion callbacks instead of the original synchronous thread.- Parameters:
target- the target exchangefilter- optional filter to only handover if filter returns true
-
done
Invoked when this unit of work has been completed, whether it has failed or completed- Parameters:
exchange- the current exchange
-
beforeRoute
Invoked when this unit of work is about to be routed by the given route.- Parameters:
exchange- the current exchangeroute- the route
-
afterRoute
Invoked when this unit of work is done being routed by the given route.- Parameters:
exchange- the current exchangeroute- the route
-
getOriginalInMessage
Message getOriginalInMessage()Gets the original INMessagethis Unit of Work was started with. The original message is only returned if the optionRuntimeConfiguration.isAllowUseOriginalMessage()is enabled. If its disabled an IllegalStateException is thrown.- Returns:
- the original IN
Message
-
isTransacted
boolean isTransacted()Are we transacted?- Returns:
- true if transacted, false otherwise
-
isTransactedBy
Are we already transacted by the given transaction key?- Parameters:
key- the transaction key- Returns:
- true if already, false otherwise
-
beginTransactedBy
Mark this UnitOfWork as being transacted by the given transaction key. When the transaction is completed then invoke theendTransactedBy(Object)method using the same key.- Parameters:
key- the transaction key
-
endTransactedBy
Mark this UnitOfWork as not transacted anymore by the given transaction definition.- Parameters:
key- the transaction key
-
getRoute
Route getRoute()Gets theRoutethat thisUnitOfWorkcurrently is being routed through. Notice that anExchangecan be routed through multiple routes and thus theRoutecan change over time.- Returns:
- the route, maybe be null if not routed through a route currently.
-
pushRoute
Pushes theRoutethat thisUnitOfWorkcurrently is being routed through. Notice that anExchangecan be routed through multiple routes and thus theRoutecan change over time.- Parameters:
route- the route
-
popRoute
Route popRoute()When finished being routed under the currentRouteit should be removed.- Returns:
- the route or null if none existed
-
routeStackLevel
int routeStackLevel()Gets theRoutelevel-of-depth that thisUnitOfWorkcurrently is being routed through. Notice that anExchangecan be routed through multiple routes and thus the level of depth can change over time. If level is 1 then the current route is at the first route (original route). Maybe be 0 if not routed through a route currently.- Returns:
- the route level-of-depth
-
routeStackLevel
int routeStackLevel(boolean includeRouteTemplate, boolean includeKamelet) Gets theRoutelevel-of-depth that thisUnitOfWorkcurrently is being routed through. Notice that anExchangecan be routed through multiple routes and thus the level of depth can change over time.If level is 1 then the current route is at the first route (original route). Maybe be 0 if not routed through a route currently.
- Parameters:
includeRouteTemplate- whether to include routes created by route templatesincludeKamelet- whether to include routes created by kamelets- Returns:
- the route level-of-depth
-
isBeforeAfterProcess
boolean isBeforeAfterProcess()Whether the unit of work should call the before/after process methods or not. -
beforeProcess
Strategy for work to be executed before processing. For example the MDCUnitOfWork leverages this to ensure MDC is handled correctly during routing exchanges using the asynchronous routing engine. This requiresisBeforeAfterProcess()returns true to be enabled.- Parameters:
processor- the processor to be executedexchange- the current exchangecallback- the callback- Returns:
- the callback to be used (can return a wrapped callback)
-
afterProcess
Strategy for work to be executed after the processing This requiresisBeforeAfterProcess()returns true to be enabled.- Parameters:
processor- the processor executedexchange- the current exchangecallback- the callback useddoneSync- whether the process was done synchronously or asynchronously
-
createChildUnitOfWork
Create a child unit of work, which is associated to this unit of work as its parent. This is often used when EIPs need to support child unit of works. For example a splitter, where the sub messages of the splitter all participate in the same sub unit of work. That sub unit of work then decides whether the Splitter (in general) is failed or a processed successfully.- Parameters:
childExchange- the child exchange- Returns:
- the created child unit of work
-
setParentUnitOfWork
Sets the parent unit of work.- Parameters:
parentUnitOfWork- the parent
-