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

public class ReplayToken extends Object implements TrackingToken, WrappedToken
Token keeping track of the position before a reset was triggered. This allows for downstream components to detect messages that are redelivered as part of a replay.
Since:
3.2
Author:
Allard Buijze
  • Method Details

    • createReplayToken

      public static TrackingToken createReplayToken(TrackingToken tokenAtReset, @Nullable TrackingToken startPosition)
      Creates a new TrackingToken that represents the given startPosition of a stream. It will be in replay state until the position of the provided tokenAtReset. After that, the tokenAtReset will become the active token and the stream will no longer be considered as replaying.
      Parameters:
      tokenAtReset - The token present when the reset was triggered
      startPosition - The position where the token should be reset to and start replaying from
      Returns:
      A token that represents a reset to the startPosition until the provided tokenAtReset
    • createReplayToken

      public static TrackingToken createReplayToken(TrackingToken tokenAtReset, TrackingToken startPosition, Object resetContext)
      Creates a new TrackingToken that represents the given startPosition of a stream. It will be in replay state until the position of the provided tokenAtReset. After that, the tokenAtReset will become the active token and the stream will no longer be considered as replaying.
      Parameters:
      tokenAtReset - The token present when the reset was triggered
      startPosition - The position where the token should be reset to and start replaying from
      resetContext - The context given to the reset, may be null
      Returns:
      A token that represents a reset to the startPosition until the provided tokenAtReset
    • createReplayToken

      public static TrackingToken createReplayToken(TrackingToken tokenAtReset)
      Creates a new TrackingToken that represents the tail of the stream. It will be in replay state until the position of the provided tokenAtReset. After that, the tokenAtReset will become the active token and the stream will no longer be considered as replaying.
      Parameters:
      tokenAtReset - The token present when the reset was triggered
      Returns:
      A token that represents a reset to the tail of the stream
    • isReplay

      public static boolean isReplay(TrackingToken trackingToken)
      Indicates whether the given trackingToken represents a position that is part of a replay.
      Parameters:
      trackingToken - The token to verify
      Returns:
      true if the token indicates a replay
    • replayContext

      public static <T> Optional<T> replayContext(TrackingToken trackingToken, @Nonnull Class<T> contextClass)
      Extracts the context from a message of the matching contextClass.

      Will resolve to an empty Optional if the provided token is not a ReplayToken, or the context isn't an instance of the provided contextClass, or there is no context at all.

      Type Parameters:
      T - The type of the context
      Parameters:
      trackingToken - The tracking token to extract the context from
      contextClass - The class the context should match
      Returns:
      The context, if present in the token
    • getTokenAtReset

      public static OptionalLong getTokenAtReset(TrackingToken trackingToken)
      Return the relative position at which a reset was triggered for this Segment. In case a replay finished or no replay is active, an OptionalLong.empty() will be returned.
      Returns:
      the relative position at which a reset was triggered for this token
    • getTokenAtReset

      public TrackingToken getTokenAtReset()
      Gets the token representing the position at which the reset was triggered.
      Returns:
      the token representing the position at which the reset was triggered
    • getCurrentToken

      public TrackingToken getCurrentToken()
      Gets the current token.
      Returns:
      the current token
    • 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
    • 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
    • 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:
    • 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
    • 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
    • context

      @Nullable public Object context()
      Returns the context that was provided when the token was reset.
      Returns:
      The context, null if none was provided during reset.
    • 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
    • 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