Class DbSchedulerDeadlineManager

java.lang.Object
org.axonframework.deadline.AbstractDeadlineManager
org.axonframework.deadline.dbscheduler.DbSchedulerDeadlineManager
All Implemented Interfaces:
DeadlineManager

public class DbSchedulerDeadlineManager extends AbstractDeadlineManager
Implementation of DeadlineManager that delegates scheduling and triggering to a db-scheduler Scheduler.

Each deadline is stored as an instance of the task named AxonDeadline, whose data is a DbSchedulerBinaryDeadlineDetails or, without useBinaryPojo, a DbSchedulerHumanReadableDeadlineDetails. The scheduler has to know the matching task, from binaryTask(Supplier) or humanReadableTask(Supplier). The details keep the layout of Axon Framework 4.13, so that tasks scheduled by Axon Framework 4 fire here, and tasks scheduled here fire on Axon Framework 4 nodes sharing the scheduler's table. The deadline's payload, metadata and scope descriptor are converted with the configured Converter, which has to match the serializer the Axon Framework 4 deadline manager used.

A fired deadline runs in a unit of work from the configured UnitOfWorkFactory, with the registered handler interceptors around its delivery to the ScopeAware components of the ScopeAwareProvider. A failing delivery fails the task, which db-scheduler then retries.

The configuration calls start() when the application starts, which starts the scheduler unless the startScheduler setting leaves that to the application. It calls shutdown() when the application stops, which stops the scheduler unless the stopScheduler setting leaves that to the application. A manager that is not part of the configuration is started and shut down by the application.


 DbSchedulerDeadlineManager deadlineManager =
         DbSchedulerDeadlineManager.builder()
                                   .scheduler(scheduler)
                                   .scopeAwareProvider(scopeAwareProvider)
                                   .unitOfWorkFactory(configuration.getComponent(UnitOfWorkFactory.class))
                                   .converter(new JacksonConverter())
                                   .build();
 
Since:
4.8.0
Author:
Gerard Klijs, Jakob Hatzl
  • Constructor Details

  • Method Details

    • builder

      public static DbSchedulerDeadlineManager.Builder builder()
      Instantiate a Builder to be able to create a DbSchedulerDeadlineManager.

      The useBinaryPojo, startScheduler and stopScheduler settings are defaulted to true. The Scheduler, ScopeAwareProvider, UnitOfWorkFactory and Converter are hard requirements and as such should be provided.

      Returns:
      a Builder to be able to create a DbSchedulerDeadlineManager
    • schedule

      public String schedule(Instant triggerDateTime, String deadlineName, @Nullable Object messageOrPayload, 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.

      Parameters:
      triggerDateTime - a Instant denoting the moment to trigger the deadline handling
      deadlineName - a String representing the name of the deadline to schedule
      messageOrPayload - 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
    • binaryTask

      public static com.github.kagkarlsson.scheduler.task.Task<DbSchedulerBinaryDeadlineDetails> binaryTask(Supplier<DbSchedulerDeadlineManager> deadlineManagerSupplier)
      Gives the Task using DbSchedulerBinaryDeadlineDetails to execute a deadline via a Scheduler. To be able to execute the task, this should be added to the task list, used to create the scheduler.
      Parameters:
      deadlineManagerSupplier - a Supplier of a DbSchedulerDeadlineManager, preferably one using dependency injection, or a DbSchedulerDeadlineManagerSupplier where that is not available
      Returns:
      a Task to execute a deadline
    • humanReadableTask

      public static com.github.kagkarlsson.scheduler.task.Task<DbSchedulerHumanReadableDeadlineDetails> humanReadableTask(Supplier<DbSchedulerDeadlineManager> deadlineManagerSupplier)
      Gives the Task using DbSchedulerHumanReadableDeadlineDetails to execute a deadline via a Scheduler. To be able to execute the task, this should be added to the task list, used to create the scheduler.
      Parameters:
      deadlineManagerSupplier - a Supplier of a DbSchedulerDeadlineManager, preferably one using dependency injection, or a DbSchedulerDeadlineManagerSupplier where that is not available
      Returns:
      a Task to execute a deadline
    • 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.
      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.
      Parameters:
      deadlineName - a String representing the name of the deadlines to cancel
    • cancelAllWithinScope

      public void cancelAllWithinScope(String deadlineName, ScopeDescriptor scope)
      Cancels all deadlines corresponding to the given deadlineName and scope. This method has no impact on deadlines which have already been triggered.

      The given scope is converted to its stored form, and compared with the stored form of each scheduled deadline's scope. The converter therefore has to produce exactly the form the stored scopes were written in. The stored class name of the scope is compared as well, as descriptors of different classes, such as an AggregateScopeDescriptor and a SagaScopeDescriptor of equal type and identifier, can have the same stored form. Axon Framework 4 compared the stored form only, and cancelled both.

      Parameters:
      deadlineName - a String representing the name of the deadlines to cancel
      scope - a ScopeDescriptor describing the scope within which the deadline was scheduled
    • start

      public void start()
      Will start the Scheduler depending on its current state and the value of startScheduler.
    • shutdown

      public void shutdown()
      Shuts down this deadline manager.

      The configuration calls this method for every DeadlineManager it holds when the application shuts down, through the DeadlineManagerLifecycleConfigurationEnhancer. As the method may be called more than once, for example by Spring as the destroy method it infers for a @Bean, implementations have to allow repeated calls: every call after the first has no effect.

      Stops the Scheduler once, unless stopScheduler is false.