Class InMemoryTaskScheduler

java.lang.Object
io.fluxcapacitor.common.InMemoryTaskScheduler
All Implemented Interfaces:
TaskScheduler

public class InMemoryTaskScheduler extends Object implements TaskScheduler
An in-memory implementation of the TaskScheduler interface, enabling scheduling and execution of tasks in a virtualized time environment. This class is suitable for deterministic testing and scenarios where precise control over task execution is required.

This scheduler uses a ScheduledExecutorService to periodically check for and execute tasks that have passed their deadlines. Tasks can be submitted for immediate execution or scheduled for execution at a specified time.

The scheduler also allows for asynchronous task execution using a provided ExecutorService or a default direct executor if one is not provided.

  • Field Details

    • defaultDelay

      public static int defaultDelay
    • defaultclock

      public static Clock defaultclock
  • Constructor Details

    • InMemoryTaskScheduler

      public InMemoryTaskScheduler()
    • InMemoryTaskScheduler

      public InMemoryTaskScheduler(Clock clock)
    • InMemoryTaskScheduler

      public InMemoryTaskScheduler(String threadName)
    • InMemoryTaskScheduler

      public InMemoryTaskScheduler(String threadName, Clock clock)
    • InMemoryTaskScheduler

      public InMemoryTaskScheduler(String threadName, Clock clock, ExecutorService workerPool)
    • InMemoryTaskScheduler

      public InMemoryTaskScheduler(int delay)
    • InMemoryTaskScheduler

      public InMemoryTaskScheduler(int delay, Clock clock)
    • InMemoryTaskScheduler

      public InMemoryTaskScheduler(int delay, String threadName)
    • InMemoryTaskScheduler

      public InMemoryTaskScheduler(int delay, String threadName, Clock clock)
    • InMemoryTaskScheduler

      public InMemoryTaskScheduler(int delay, String threadName, Clock clock, ExecutorService workerPool)
  • Method Details

    • executeExpiredTasks

      public void executeExpiredTasks()
      Description copied from interface: TaskScheduler
      Executes any scheduled tasks that are due according to the scheduler's current time.

      Useful for test scenarios or manual triggering of scheduled behavior.

      Specified by:
      executeExpiredTasks in interface TaskScheduler
    • executeExpiredTasksAsync

      public void executeExpiredTasksAsync()
    • tryRunTask

      protected void tryRunTask(InMemoryTaskScheduler.Task task)
    • submit

      public void submit(ThrowingRunnable task)
      Description copied from interface: TaskScheduler
      Immediately schedules a task for execution.
      Specified by:
      submit in interface TaskScheduler
      Parameters:
      task - the task to execute
    • schedule

      public Registration schedule(long deadline, ThrowingRunnable task)
      Description copied from interface: TaskScheduler
      Schedules a task to be executed at the given epoch millisecond timestamp.
      Specified by:
      schedule in interface TaskScheduler
      Parameters:
      deadline - epoch milliseconds (UTC) representing the execution time
      task - the task to execute
      Returns:
      a Registration to cancel the task if needed
    • clock

      public Clock clock()
      Description copied from interface: TaskScheduler
      Returns the clock associated with this scheduler. This clock is used to determine the current time for scheduling.

      In test scenarios, the clock may be a mock or virtualized instance to support deterministic test behavior.

      Specified by:
      clock in interface TaskScheduler
      Returns:
      the Clock used by this scheduler
    • shutdown

      public void shutdown()
      Description copied from interface: TaskScheduler
      Shuts down the scheduler and stops execution of any pending or future tasks.

      In production use, this ensures proper resource cleanup. In test environments, this may reset scheduler state.

      Specified by:
      shutdown in interface TaskScheduler