Interface UnitOfWork

  • All Superinterfaces:
    AutoCloseable, Service

    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 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
      • containsSynchronization

        boolean containsSynchronization​(Synchronization synchronization)
        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

        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
      • handoverSynchronization

        void handoverSynchronization​(Exchange target,
                                     Predicate<Synchronization> filter)
        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
        filter - optional filter to only handover if filter returns true
      • 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
      • beforeRoute

        void beforeRoute​(Exchange exchange,
                         Route route)
        Invoked when this unit of work is about to be routed by the given route.
        Parameters:
        exchange - the current exchange
        route - the route
      • afterRoute

        void afterRoute​(Exchange exchange,
                        Route route)
        Invoked when this unit of work is done being routed by the given route.
        Parameters:
        exchange - the current exchange
        route - the route
      • getOriginalInMessage

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

        The original message is only returned if the option RuntimeConfiguration.isAllowUseOriginalMessage() is enabled. If its disabled an IllegalStateException is thrown.

        Returns:
        the original IN Message, or null if using original message is disabled.
      • 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
      • getRoute

        Route getRoute()
        Gets the Route that this UnitOfWork currently is being routed through.

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

        Returns:
        the route, maybe be null if not routed through a route currently.
      • pushRoute

        void pushRoute​(Route route)
        Pushes the Route that this UnitOfWork currently is being routed through.

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

        Parameters:
        route - the route
      • popRoute

        Route popRoute()
        When finished being routed under the current Route it should be removed.
        Returns:
        the route or null if none existed
      • isBeforeAfterProcess

        boolean isBeforeAfterProcess()
        Whether the unit of work should call the before/after process methods or not.
      • beforeProcess

        AsyncCallback beforeProcess​(Processor processor,
                                    Exchange exchange,
                                    AsyncCallback callback)
        Strategy for 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.

        This requires isBeforeAfterProcess() returns true to be enabled.

        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 work to be executed after the processing

        This requires isBeforeAfterProcess() returns true to be enabled.

        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 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

        void setParentUnitOfWork​(UnitOfWork parentUnitOfWork)
        Sets the parent unit of work.
        Parameters:
        parentUnitOfWork - the parent