Interface TaskScheduler
- All Known Implementing Classes:
InMemoryTaskScheduler
TaskScheduler
enables deferred execution of tasks based on a deadline or delay. It is used by Flux
Capacitor internally, but can also be applied directly for scheduling logic within applications.
Unlike typical thread-based scheduling (e.g. ScheduledExecutorService
), this abstraction allows
fine-grained control over when tasks are executed, making it ideal for use in test fixtures and simulations
where real-time waiting would be undesirable.
-
Method Summary
Modifier and TypeMethodDescriptionclock()
Returns the clock associated with this scheduler.void
Executes any scheduled tasks that are due according to the scheduler's current time.default <R> CompletableFuture
<R> orTimeout
(CompletableFuture<R> future, Duration timeout) Sets a timeout for aCompletableFuture
.schedule
(long deadline, ThrowingRunnable task) Schedules a task to be executed at the given epoch millisecond timestamp.default Registration
schedule
(Duration duration, ThrowingRunnable task) Schedules a task to be executed after a specified delay.default Registration
schedule
(Instant deadline, ThrowingRunnable task) Schedules a task to be executed at a specific timestamp.void
shutdown()
Shuts down the scheduler and stops execution of any pending or future tasks.default void
submit
(ThrowingRunnable task) Immediately schedules a task for execution.
-
Method Details
-
submit
Immediately schedules a task for execution.- Parameters:
task
- the task to execute
-
schedule
Schedules a task to be executed after a specified delay.- Parameters:
duration
- the delay duration after which the task should be executedtask
- the task to execute- Returns:
- a
Registration
that can be used to cancel the task before execution
-
schedule
Schedules a task to be executed at a specific timestamp.- Parameters:
deadline
- theInstant
timestamp indicating when to execute the tasktask
- the task to execute- Returns:
- a
Registration
that can be used to cancel the task before execution
-
schedule
Schedules a task to be executed at the given epoch millisecond timestamp.- Parameters:
deadline
- epoch milliseconds (UTC) representing the execution timetask
- the task to execute- Returns:
- a
Registration
to cancel the task if needed
-
orTimeout
Sets a timeout for aCompletableFuture
. If the future does not complete before the timeout expires, it is completed exceptionally with aTimeoutException
.The returned future will still complete normally if the underlying task completes first.
- Type Parameters:
R
- the type of the result- Parameters:
future
- the future to observetimeout
- the maximum duration to wait before timing out- Returns:
- the same future instance
-
clock
Clock clock()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.
- Returns:
- the
Clock
used by this scheduler
-
executeExpiredTasks
void executeExpiredTasks()Executes any scheduled tasks that are due according to the scheduler's current time.Useful for test scenarios or manual triggering of scheduled behavior.
-
shutdown
void shutdown()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.
-