public class Scheduler extends Object
This class is an internal implementation detail only intended for use within spectator. It is subject to change without notice.
Simple scheduler for recurring tasks based on a fixed size thread pool. This class is mostly intended for running short lived tasks at a regular interval.
Usage
Scheduler scheduler = new Scheduler(registry, "spectator-polling", 2); Scheduler.Options options = new Scheduler.Options() .withFrequency(Scheduler.Policy.FIXED_RATE_SKIP_IF_LONG, Duration.ofSeconds(10)); scheduler.schedule(options, () -> doWork());
Metrics
The following metrics can be used to monitor the behavior of the scheduler:spectator.scheduler.queueSize
: gauge reporting the number of
items in the queue. Note, that for repeating tasks the items will almost
always be in queue except during execution.spectator.scheduler.poolSize
: gauge reporting the number of
threads available in the pool.spectator.scheduler.activeThreads
: gauge reporting the number of
threads that are currently executing a task.spectator.scheduler.taskExecutionTime
: timer reporting the
execution time of an individual task.spectator.scheduler.taskExecutionDelay
: timer reporting the
delay between the desired execution time of a task and when it was actually
executed. A high execution delay means that the scheduler cannot keep up
with the amount of work. This might indicate more threads are needed.spectator.scheduler.skipped
: counter reporting the number of
executions that were skipped because the task did not complete before the
next scheduled execution time.spectator.scheduler.uncaughtExceptions
: counter reporting the
number of times an exception is propagated out from a task.id
dimension to distinguish a particular scheduler
instance.Modifier and Type | Class and Description |
---|---|
static class |
Scheduler.Options
Options to control how a task will get executed.
|
static class |
Scheduler.Policy
Repetition schedulingPolicy for scheduled tasks.
|
Constructor and Description |
---|
Scheduler(Registry registry,
String id,
int poolSize)
Create a new instance.
|
Modifier and Type | Method and Description |
---|---|
ScheduledFuture<?> |
schedule(Scheduler.Options options,
Runnable task)
Schedule a repetitive task.
|
void |
shutdown()
Shutdown and cleanup resources associated with the scheduler.
|
public Scheduler(Registry registry, String id, int poolSize)
registry
- Registry to use for collecting metrics. The clock from the registry will also be
used as the clock source for accessing the time.id
- Id for this instance of the scheduler. Used to distinguish between instances of
the scheduler for metrics and thread names. Threads will be named as
spectator-$id-$i
.poolSize
- Number of threads to have in the pool. The threads will not be started until the
first task is scheduled.public ScheduledFuture<?> schedule(Scheduler.Options options, Runnable task)
options
- Options for controlling the execution of the task. See Scheduler.Options
for more information.task
- Task to execute.public void shutdown()