java.lang.Object
org.axonframework.messaging.eventhandling.processing.streaming.token.MergedTrackingToken
All Implemented Interfaces:
TrackingToken, WrappedToken

public class MergedTrackingToken extends Object implements TrackingToken, WrappedToken
Special Wrapped Token implementation that keeps track of two separate tokens, of which the streams have been merged into a single one. This token keeps track of the progress of the two original "halves", by advancing each individually, until both halves represent the same position.
Since:
4.1
Author:
Allard Buijze
  • Constructor Details

    • MergedTrackingToken

      @ConstructorProperties({"lowerSegmentToken","upperSegmentToken"}) public MergedTrackingToken(TrackingToken lowerSegmentToken, TrackingToken upperSegmentToken)
      Initialize a Merged Token, with the lowerSegmentToken representing the progress of the segment with the lower segmentId, and upperSegmentToken representing the progress of the segment with the higher segmentId.
      Parameters:
      lowerSegmentToken - the token of the half with the lower segment ID
      upperSegmentToken - the token of the half with the higher segment ID
    • MergedTrackingToken

      protected MergedTrackingToken(TrackingToken lowerSegmentToken, TrackingToken upperSegmentToken, boolean lowerSegmentAdvanced, boolean upperSegmentAdvanced)
      Initialize a Merged Token, with the lowerSegmentToken representing the progress of the segment with the lower segmentId, and upperSegmentToken representing the progress of the segment with the higher segmentId, additionally indicating if either of these segments were advanced by the latest call to advancedTo(TrackingToken)
      Parameters:
      lowerSegmentToken - the token of the half with the lower segment ID
      upperSegmentToken - the token of the half with the higher segment ID
      lowerSegmentAdvanced - whether the lower segment advanced in the last call
      upperSegmentAdvanced - whether the upper segment advanced in the last call
  • Method Details

    • merged

      public static TrackingToken merged(TrackingToken lowerSegmentToken, TrackingToken upperSegmentToken)
      Create a merged token using the given lowerSegmentToken and upperSegmentToken.
      Parameters:
      lowerSegmentToken - the token of the half with the lower segment ID
      upperSegmentToken - the token of the half with the higher segment ID
      Returns:
      a token representing the position of the merger of both tokens
    • isMergeInProgress

      public static boolean isMergeInProgress(TrackingToken trackingToken)
      Indicates whether the given trackingToken represents a token that is part of a merge.
      Parameters:
      trackingToken - the token to verify
      Returns:
      true if the token indicates a merge
    • mergePosition

      public static OptionalLong mergePosition(TrackingToken trackingToken)
      Return the estimated relative token position this Segment will have after a merge operation is complete. In case no estimation can be given or no merge in progress, an OptionalLong.empty() will be returned.
      Returns:
      the estimated relative position this Segment will reach after a merge operation is complete.
    • lowerBound

      public TrackingToken lowerBound(TrackingToken other)
      Description copied from interface: TrackingToken
      Returns a token that represents the lower bound between this and the other token. Effectively, the returned token will cause events not received by both this and the other token to be redelivered.
      Specified by:
      lowerBound in interface TrackingToken
      Parameters:
      other - The token to compare to this one
      Returns:
      The token representing the lower bound of the two
    • position

      public OptionalLong position()
      Description copied from interface: TrackingToken
      Return the estimated relative position this token represents. In case no estimation can be given an OptionalLong.empty() will be returned.
      Specified by:
      position in interface TrackingToken
      Returns:
      the estimated relative position of this token
    • upperBound

      public TrackingToken upperBound(TrackingToken other)
      Description copied from interface: TrackingToken
      Returns the token that represents the furthest possible position in a stream that either this token or the given other represents. Effectively, this means this token will only deliver events that neither this, nor the other have been received.
      Specified by:
      upperBound in interface TrackingToken
      Parameters:
      other - The token to compare this token to
      Returns:
      a token that represents the furthest position of this or the other stream
    • covers

      public boolean covers(TrackingToken other)
      Description copied from interface: TrackingToken
      Indicates whether this token covers the other token completely. That means that this token represents a position in a stream that has received all the events that a stream represented by the other token has received.

      Note that this operation is only safe when comparing tokens obtained from events from the same StreamableEventSource.

      Specified by:
      covers in interface TrackingToken
      Parameters:
      other - The token to compare to this one
      Returns:
      true if this token covers the other, otherwise false
    • samePositionAs

      public boolean samePositionAs(TrackingToken other)
      Description copied from interface: TrackingToken
      Indicates whether this token is at the exact same spot in the event stream as the other token.

      This method is particularly useful when comparing tokens from different points in time, such as during replay detection, where token implementations may naturally differ.

      By default, this method checks bidirectional coverage: this.covers(other) && other.covers(this), which ensures both tokens are at the same position.

      Specified by:
      samePositionAs in interface TrackingToken
      Parameters:
      other - The token to validate against this token.
      Returns:
      true if this token is at the same location as the other token, otherwise false. Returns false if other is null.
      See Also:
    • advancedTo

      public TrackingToken advancedTo(TrackingToken newToken)
      Description copied from interface: WrappedToken
      Advance this token to the given newToken.
      Specified by:
      advancedTo in interface WrappedToken
      Parameters:
      newToken - The token representing the position to advance to
      Returns:
      a token representing the new position
    • unwrap

      public <R extends TrackingToken> Optional<R> unwrap(Class<R> tokenType)
      Description copied from interface: WrappedToken
      Retrieve a token of given tokenType if it is wrapped by this token.
      Specified by:
      unwrap in interface WrappedToken
      Type Parameters:
      R - The generic type of the token to unwrap to
      Parameters:
      tokenType - The type of token to unwrap to
      Returns:
      an optional with the unwrapped token, if found
    • lowerBound

      public TrackingToken lowerBound()
      Description copied from interface: WrappedToken
      Returns the token representing the current position in the stream.
      Specified by:
      lowerBound in interface WrappedToken
      Returns:
      the token representing the current position in the stream
    • upperBound

      public TrackingToken upperBound()
      Description copied from interface: WrappedToken
      Returns the token representing the furthest position in the stream described by this token. This is usually a position that has been (partially) processed before.
      Specified by:
      upperBound in interface WrappedToken
      Returns:
      the token representing the furthest position reached in the stream
    • lowerSegmentToken

      public TrackingToken lowerSegmentToken()
      Returns the token indicating the progress of the lower half (the half with the lower segmentId) of the merged segment represented by this token
      Returns:
      the token indicating the progress of the lower half of the merged segment
    • upperSegmentToken

      public TrackingToken upperSegmentToken()
      Returns the token indicating the progress of the upper half (the half with the higher segmentId) of the merged segment represented by this token
      Returns:
      the token indicating the progress of the upper half of the merged segment
    • isLowerSegmentAdvanced

      public boolean isLowerSegmentAdvanced()
      Indicates whether the last call to advancedTo(TrackingToken) caused the lower segment to advance
      Returns:
      true if the last advancement moved the lower segment
    • isUpperSegmentAdvanced

      public boolean isUpperSegmentAdvanced()
      Indicates whether the last call to advancedTo(TrackingToken) caused the upper segment to advance
      Returns:
      true if the last advancement moved the upper segment
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object