Package io.fluxcapacitor.common
Class InMemoryTaskScheduler
java.lang.Object
io.fluxcapacitor.common.InMemoryTaskScheduler
- All Implemented Interfaces:
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.
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionInMemoryTaskScheduler
(int delay) InMemoryTaskScheduler
(int delay, String threadName) InMemoryTaskScheduler
(int delay, String threadName, Clock clock) InMemoryTaskScheduler
(int delay, String threadName, Clock clock, ExecutorService workerPool) InMemoryTaskScheduler
(int delay, Clock clock) InMemoryTaskScheduler
(String threadName) InMemoryTaskScheduler
(String threadName, Clock clock) InMemoryTaskScheduler
(String threadName, Clock clock, ExecutorService workerPool) InMemoryTaskScheduler
(Clock clock) -
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.void
schedule
(long deadline, ThrowingRunnable task) Schedules a task to be executed at the given epoch millisecond timestamp.void
shutdown()
Shuts down the scheduler and stops execution of any pending or future tasks.void
submit
(ThrowingRunnable task) Immediately schedules a task for execution.protected void
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.fluxcapacitor.common.TaskScheduler
orTimeout, schedule, schedule
-
Field Details
-
defaultDelay
public static int defaultDelay -
defaultclock
-
-
Constructor Details
-
InMemoryTaskScheduler
public InMemoryTaskScheduler() -
InMemoryTaskScheduler
-
InMemoryTaskScheduler
-
InMemoryTaskScheduler
-
InMemoryTaskScheduler
-
InMemoryTaskScheduler
public InMemoryTaskScheduler(int delay) -
InMemoryTaskScheduler
-
InMemoryTaskScheduler
-
InMemoryTaskScheduler
-
InMemoryTaskScheduler
-
-
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 interfaceTaskScheduler
-
executeExpiredTasksAsync
public void executeExpiredTasksAsync() -
tryRunTask
-
submit
Description copied from interface:TaskScheduler
Immediately schedules a task for execution.- Specified by:
submit
in interfaceTaskScheduler
- Parameters:
task
- the task to execute
-
schedule
Description copied from interface:TaskScheduler
Schedules a task to be executed at the given epoch millisecond timestamp.- Specified by:
schedule
in interfaceTaskScheduler
- Parameters:
deadline
- epoch milliseconds (UTC) representing the execution timetask
- the task to execute- Returns:
- a
Registration
to cancel the task if needed
-
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 interfaceTaskScheduler
- 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 interfaceTaskScheduler
-