org.apache.camel.spi
Interface UnitOfWork

All Superinterfaces:
Service
All Known Implementing Classes:
DefaultUnitOfWork, MDCUnitOfWork

public interface UnitOfWork
extends Service

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.


Method Summary
 void addSynchronization(Synchronization synchronization)
          Adds a synchronization hook
 void afterProcess(Processor processor, Exchange exchange, AsyncCallback callback, boolean doneSync)
          Strategy for optional work to be executed after the processing
 AsyncCallback beforeProcess(Processor processor, Exchange exchange, AsyncCallback callback)
          Strategy for optional work to be execute before processing

For example the MDCUnitOfWork leverages this to ensure MDC is handled correctly during routing exchanges using the asynchronous routing engine.

 void beginSubUnitOfWork(Exchange exchange)
          Begins a SubUnitOfWork, where sub (child) unit of works participate in a parent unit of work.
 void beginTransactedBy(Object key)
          Mark this UnitOfWork as being transacted by the given transaction key.
 UnitOfWork createChildUnitOfWork(Exchange childExchange)
          Create a child unit of work, which is associated to this unit of work as its parent.
 void done(Exchange exchange)
          Invoked when this unit of work has been completed, whether it has failed or completed
 void endSubUnitOfWork(Exchange exchange)
          Ends a SubUnitOfWork.
 void endTransactedBy(Object key)
          Mark this UnitOfWork as not transacted anymore by the given transaction definition.
 String getId()
          Returns the unique ID of this unit of work, lazily creating one if it does not yet have one
 Message getOriginalInMessage()
          Gets the original IN Message this Unit of Work was started with.
 RouteContext getRouteContext()
          Gets the RouteContext that this UnitOfWork currently is being routed through.
 SubUnitOfWorkCallback getSubUnitOfWorkCallback()
          Gets the SubUnitOfWorkCallback if this unit of work participates in a sub unit of work.
 TracedRouteNodes getTracedRouteNodes()
          Gets tracing information
 void handoverSynchronization(Exchange target)
          Handover all the registered synchronizations to the target Exchange.
 boolean isTransacted()
          Are we transacted?
 boolean isTransactedBy(Object key)
          Are we already transacted by the given transaction key?
 RouteContext popRouteContext()
          When finished being routed under the current RouteContext it should be removed.
 void pushRouteContext(RouteContext routeContext)
          Pushes the RouteContext that this UnitOfWork currently is being routed through.
 void removeSynchronization(Synchronization synchronization)
          Removes a synchronization hook
 void setParentUnitOfWork(UnitOfWork parentUnitOfWork)
          Sets the parent unit of work.
 
Methods inherited from interface org.apache.camel.Service
start, stop
 

Method Detail

addSynchronization

void addSynchronization(Synchronization synchronization)
Adds a synchronization hook

Parameters:
synchronization - the hook

removeSynchronization

void removeSynchronization(Synchronization synchronization)
Removes a synchronization hook

Parameters:
synchronization - the hook

handoverSynchronization

void handoverSynchronization(Exchange target)
Handover all the registered synchronizations to the target Exchange.

This is used when a route turns into asynchronous and the Exchange that 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

done

void done(Exchange exchange)
Invoked when this unit of work has been completed, whether it has failed or completed

Parameters:
exchange - the current exchange

getId

String getId()
Returns the unique ID of this unit of work, lazily creating one if it does not yet have one

Returns:
the unique ID

getOriginalInMessage

Message getOriginalInMessage()
Gets the original IN Message this Unit of Work was started with.

Returns:
the original IN Message

getTracedRouteNodes

TracedRouteNodes getTracedRouteNodes()
Gets tracing information

Returns:
trace information

isTransacted

boolean isTransacted()
Are we transacted?

Returns:
true if transacted, false otherwise

isTransactedBy

boolean isTransactedBy(Object key)
Are we already transacted by the given transaction key?

Parameters:
key - the transaction key
Returns:
true if already, false otherwise

beginTransactedBy

void beginTransactedBy(Object key)
Mark this UnitOfWork as being transacted by the given transaction key.

When the transaction is completed then invoke the endTransactedBy(Object) method using the same key.

Parameters:
key - the transaction key

endTransactedBy

void endTransactedBy(Object key)
Mark this UnitOfWork as not transacted anymore by the given transaction definition.

Parameters:
key - the transaction key

getRouteContext

RouteContext getRouteContext()
Gets the RouteContext that this UnitOfWork currently is being routed through.

Notice that an Exchange can be routed through multiple routes and thus the RouteContext can change over time.

Returns:
the route context
See Also:
pushRouteContext(RouteContext), popRouteContext()

pushRouteContext

void pushRouteContext(RouteContext routeContext)
Pushes the RouteContext that this UnitOfWork currently is being routed through.

Notice that an Exchange can be routed through multiple routes and thus the RouteContext can change over time.

Parameters:
routeContext - the route context

popRouteContext

RouteContext popRouteContext()
When finished being routed under the current RouteContext it should be removed.

Returns:
the route context or null if none existed

beforeProcess

AsyncCallback beforeProcess(Processor processor,
                            Exchange exchange,
                            AsyncCallback callback)
Strategy for optional work to be execute before processing

For example the MDCUnitOfWork leverages this to ensure MDC is handled correctly during routing exchanges using the asynchronous routing engine.

Parameters:
processor - the processor to be executed
exchange - the current exchange
callback - the callback
Returns:
the callback to be used (can return a wrapped callback)

afterProcess

void afterProcess(Processor processor,
                  Exchange exchange,
                  AsyncCallback callback,
                  boolean doneSync)
Strategy for optional work to be executed after the processing

Parameters:
processor - the processor executed
exchange - the current exchange
callback - the callback used
doneSync - whether the process was done synchronously or asynchronously

createChildUnitOfWork

UnitOfWork createChildUnitOfWork(Exchange childExchange)
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 SubUnitOfWorks. 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
See Also:
SubUnitOfWork, SubUnitOfWorkCallback

setParentUnitOfWork

void setParentUnitOfWork(UnitOfWork parentUnitOfWork)
Sets the parent unit of work.

Parameters:
parentUnitOfWork - the parent

getSubUnitOfWorkCallback

SubUnitOfWorkCallback getSubUnitOfWorkCallback()
Gets the SubUnitOfWorkCallback if this unit of work participates in a sub unit of work.

Returns:
the callback, or null if this unit of work is not part of a sub unit of work.
See Also:
beginSubUnitOfWork(org.apache.camel.Exchange)

beginSubUnitOfWork

void beginSubUnitOfWork(Exchange exchange)
Begins a SubUnitOfWork, where sub (child) unit of works participate in a parent unit of work. The SubUnitOfWork will callback to the parent unit of work using SubUnitOfWorkCallbacks.

Parameters:
exchange - the exchange

endSubUnitOfWork

void endSubUnitOfWork(Exchange exchange)
Ends a SubUnitOfWork.

The beginSubUnitOfWork(org.apache.camel.Exchange) must have been invoked prior to this operation.

Parameters:
exchange - the exchange


Apache CAMEL