Class StubDeadlineManager

java.lang.Object
org.axonframework.test.deadline.StubDeadlineManager
All Implemented Interfaces:
DeadlineManager

public class StubDeadlineManager extends Object implements DeadlineManager
Stub implementation of DeadlineManager. Records all scheduled, canceled and met deadlines.

Nothing fires on its own: a test advances the stub's time with advanceTimeBy(Duration, DeadlineConsumer) or advanceTimeTo(Instant, DeadlineConsumer), which hands every deadline that is due by then to the given DeadlineConsumer. Each fired deadline runs in a unit of work from the UnitOfWorkFactory this stub was built with, with the registered handler interceptors around the consumer.

The SagaTestFixture registers a stub as its DeadlineManager, so a Saga handler that takes a DeadlineManager parameter schedules on it, and the fixture's deadline assertions read what it recorded.

Since:
3.3
Author:
Milan Savic, Steven van Beelen
  • Constructor Details

    • StubDeadlineManager

      public StubDeadlineManager()
      Initializes the manager with ZonedDateTime.now() as current time.
    • StubDeadlineManager

      public StubDeadlineManager(TemporalAccessor currentDateTime)
      Initializes the manager with provided currentDateTime as current time.

      Fired deadlines run in a unit of work bound to no application, the counterpart of the plain unit of work Axon Framework 4 started. A consumer that needs components from the context, such as a Saga dispatching a command, needs the constructor taking the configuration's UnitOfWorkFactory instead.

      Parameters:
      currentDateTime - the instance to use as current date and time
    • StubDeadlineManager

      public StubDeadlineManager(TemporalAccessor currentDateTime, UnitOfWorkFactory unitOfWorkFactory)
      Initializes the manager with provided currentDateTime as current time, firing deadlines in units of work from the given unitOfWorkFactory.

      New in Axon Framework 5, where a unit of work is created by a factory rather than started on the current thread. Passing the configuration's UnitOfWorkFactory gives a fired deadline the same components a handler gets anywhere else in that configuration.

      Parameters:
      currentDateTime - the instant to use as current date and time
      unitOfWorkFactory - the factory of the unit of work each fired deadline runs in
  • Method Details

    • initializeAt

      public void initializeAt(TemporalAccessor currentDateTime) throws IllegalStateException
      Resets the initial "current time" of this manager. Must be called before any deadlines are scheduled.
      Parameters:
      currentDateTime - the instant to use as the current date and time
      Throws:
      IllegalStateException - when calling this method after deadlines are scheduled
    • schedule

      public String schedule(Instant triggerDateTime, String deadlineName, @Nullable Object payloadOrMessage, ScopeDescriptor deadlineScope)
      Description copied from interface: DeadlineManager
      Schedules a deadline at given triggerDateTime with provided context. The returned scheduleId and provided deadlineName combination can be used to cancel the scheduled deadline.

      The given messageOrPayload may be any object, as well as a DeadlineMessage. In the latter case, the instance provided is the donor for the payload and Metadata of the actual deadline being used. In the former case, the given messageOrPayload will be wrapped as the payload of a DeadlineMessage.

      Specified by:
      schedule in interface DeadlineManager
      Parameters:
      triggerDateTime - a Instant denoting the moment to trigger the deadline handling
      deadlineName - a String representing the name of the deadline to schedule
      payloadOrMessage - a Message or payload for a message as an Object
      deadlineScope - a ScopeDescriptor describing the scope within which the deadline was scheduled
      Returns:
      the scheduleId as a String to use when cancelling the schedule
    • schedule

      public String schedule(Duration triggerDuration, String deadlineName, @Nullable Object payloadOrMessage, ScopeDescriptor deadlineScope)
      Description copied from interface: DeadlineManager
      Schedules a deadline after the given triggerDuration with provided context. The provided deadlineName / scheduleId combination can be used to cancel the scheduled deadline.

      The given messageOrPayload may be any object, as well as a DeadlineMessage. In the latter case, the instance provided is the donor for the payload and Metadata of the actual deadline being used. In the former case, the given messageOrPayload will be wrapped as the payload of a DeadlineMessage.

      Specified by:
      schedule in interface DeadlineManager
      Parameters:
      triggerDuration - a Duration describing the waiting period before handling the deadline
      deadlineName - a String representing the name of the deadline to schedule
      payloadOrMessage - a Message or payload for a message as an Object
      deadlineScope - a ScopeDescriptor describing the scope within which the deadline was scheduled
      Returns:
      the scheduleId as a String to use when cancelling the schedule
    • cancelSchedule

      public void cancelSchedule(String deadlineName, String scheduleId)
      Description copied from interface: DeadlineManager
      Cancels the deadline corresponding to the given deadlineName / scheduleId combination. This method has no impact on deadlines which have already been triggered.
      Specified by:
      cancelSchedule in interface DeadlineManager
      Parameters:
      deadlineName - a String representing the name of the deadline to cancel
      scheduleId - the String denoting the scheduled deadline to cancel
    • cancelAll

      public void cancelAll(String deadlineName)
      Description copied from interface: DeadlineManager
      Cancels all the deadlines corresponding to the given deadlineName. This method has no impact on deadlines which have already been triggered.
      Specified by:
      cancelAll in interface DeadlineManager
      Parameters:
      deadlineName - a String representing the name of the deadlines to cancel
    • cancelAllWithinScope

      public void cancelAllWithinScope(String deadlineName, ScopeDescriptor scope)
      Description copied from interface: DeadlineManager
      Cancels all deadlines corresponding to the given deadlineName and scope. This method has no impact on deadlines which have already been triggered.
      Specified by:
      cancelAllWithinScope in interface DeadlineManager
      Parameters:
      deadlineName - a String representing the name of the deadlines to cancel
      scope - a ScopeDescriptor describing the scope within which the deadline was scheduled
    • getScheduledDeadlines

      public List<ScheduledDeadlineInfo> getScheduledDeadlines()
      Return all scheduled deadlines which have not been met (yet).
      Returns:
      all scheduled deadlines which have not been met (yet)
    • getDeadlinesMet

      @Deprecated public List<ScheduledDeadlineInfo> getDeadlinesMet()
      Deprecated.
      Return all deadlines which have been met.
      Returns:
      all deadlines which have been met
    • getTriggeredDeadlines

      public List<ScheduledDeadlineInfo> getTriggeredDeadlines()
      Return all triggered deadlines.
      Returns:
      all triggered deadlines
    • getCurrentDateTime

      public Instant getCurrentDateTime()
      Return the current date and time as an Instant as is being used by this DeadlineManager.
      Returns:
      the current date and time used by the manager
    • advanceToNextTrigger

      public ScheduledDeadlineInfo advanceToNextTrigger()
      Advances the "current time" of the manager to the next scheduled deadline, and returns that deadline. In theory, this may cause "current time" to move backwards.
      Returns:
      ScheduledDeadlineInfo of the first scheduled deadline
    • advanceTimeTo

      public void advanceTimeTo(Instant newDateTime, DeadlineConsumer deadlineConsumer)
      Advances time to the given newDateTime and invokes the given deadlineConsumer for each deadline scheduled until that time.
      Parameters:
      newDateTime - the time to advance the "current time" of the manager to
      deadlineConsumer - the consumer to invoke for each deadline to trigger
    • advanceTimeBy

      public void advanceTimeBy(Duration duration, DeadlineConsumer deadlineConsumer)
      Advances time by the given duration and invokes the given deadlineConsumer for each deadline scheduled until that time.
      Parameters:
      duration - the amount of time to advance the "current time" of the manager with
      deadlineConsumer - the consumer to invoke for each deadline to trigger
    • registerDispatchInterceptor

      public Registration registerDispatchInterceptor(MessageDispatchInterceptor<? super DeadlineMessage> dispatchInterceptor)
      Registers the given dispatchInterceptor, which is invoked for each deadline that is scheduled.
      Parameters:
      dispatchInterceptor - the interceptor deciding which deadline message is scheduled
      Returns:
      a registration to remove the interceptor again
    • registerHandlerInterceptor

      public Registration registerHandlerInterceptor(MessageHandlerInterceptor<? super DeadlineMessage> handlerInterceptor)
      Registers the given handlerInterceptor, which is invoked around the consumer of each fired deadline.
      Parameters:
      handlerInterceptor - the interceptor to invoke around the consumer of a fired deadline
      Returns:
      a registration to remove the interceptor again