Class DelayedTask

java.lang.Object
org.axonframework.update.common.DelayedTask

@Internal public class DelayedTask extends Object
A utility class to run a task with a delay in a virtual thread. The task can be checked for its status (started, finished, failed), and the failure cause can be retrieved if it failed.

This has severely lower overhead than using a ScheduledExecutorService or similar constructs, as it does not require a thread pool or scheduling mechanism. It simply runs the task in a virtual thread after the specified delay. The virtual thread is parked for the duration of the delay, and is executed on a carrier thread when ready, which allows it to be lightweight and efficient.

Since:
5.0.0
Author:
Mitchell Herrijgers
  • Method Details

    • of

      public static DelayedTask of(@Nonnull Runnable runnable, long delay)
      Creates a new DelayedTask that runs the given runnable after the specified delay.
      Parameters:
      runnable - The task to run after the delay.
      delay - The delay in milliseconds before the task is executed. Must be non-negative.
      Returns:
      A new instance of DelayedTask that will run the given runnable after the specified delay.
    • isStarted

      public boolean isStarted()
      Whether the task has started running.
      Returns:
      true if the task has started, false otherwise.
    • isFinished

      public boolean isFinished()
      Whether the task has finished running.
      Returns:
      true if the task has finished, false otherwise.
    • isFailed

      public boolean isFailed()
      Whether the task has failed.
      Returns:
      true if the task has failed, false otherwise.
    • getFailureCause

      public Exception getFailureCause()
      Gets the cause of the failure if the task has failed.
      Returns:
      The exception that caused the failure, or null if the task has not failed.
    • cancel

      public void cancel()
      Cancels the task if it is still running. If the task has already started, it will not be interrupted. If the task is currently sleeping, it will be interrupted and will not run.