Package com.github.kagkarlsson.scheduler
Class Scheduler
- java.lang.Object
-
- com.github.kagkarlsson.scheduler.Scheduler
-
- All Implemented Interfaces:
SchedulerClient
- Direct Known Subclasses:
ManualScheduler
public class Scheduler extends Object implements SchedulerClient
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.github.kagkarlsson.scheduler.SchedulerClient
SchedulerClient.Builder, SchedulerClient.ScheduleOptions, SchedulerClient.SchedulerClientName, SchedulerClient.StandardSchedulerClient
-
-
Field Summary
Fields Modifier and Type Field Description protected PollStrategyexecuteDueStrategyprotected Executorexecutorprotected List<OnStartup>onStartupstatic StringTHREAD_PREFIXstatic doubleTRIGGER_NEXT_BATCH_WHEN_AVAILABLE_THREADS_RATIO-
Fields inherited from interface com.github.kagkarlsson.scheduler.SchedulerClient
DEFAULT_BATCH_SIZE
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedScheduler(Clock clock, TaskRepository schedulerTaskRepository, TaskRepository clientTaskRepository, TaskResolver taskResolver, int threadpoolSize, ExecutorService executorService, SchedulerName schedulerName, Waiter executeDueWaiter, Duration heartbeatInterval, int numberOfMissedHeartbeatsBeforeDead, List<SchedulerListener> schedulerListeners, List<ExecutionInterceptor> executionInterceptors, PollingStrategyConfig pollingStrategyConfig, Duration deleteUnresolvedAfter, Duration shutdownMaxWait, LogLevel logLevel, boolean logStackTrace, List<OnStartup> onStartup, ExecutorService dueExecutor, ScheduledExecutorService housekeeperExecutor)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcancel(TaskInstanceId taskInstanceId)Removes/Cancels an execution.static SchedulerBuildercreate(DataSource dataSource, Task<?>... knownTasks)static SchedulerBuildercreate(DataSource dataSource, List<Task<?>> knownTasks)protected voiddetectDeadExecutions()protected voidexecuteDue()protected voidexecuteOnStartup()voidfetchScheduledExecutions(ScheduledExecutionsFilter filter, Consumer<ScheduledExecution<Object>> consumer)voidfetchScheduledExecutions(Consumer<ScheduledExecution<Object>> consumer)Gets all scheduled executions and supplies them to the provided Consumer.<T> voidfetchScheduledExecutionsForTask(String taskName, Class<T> dataClass, ScheduledExecutionsFilter filter, Consumer<ScheduledExecution<T>> consumer)<T> voidfetchScheduledExecutionsForTask(String taskName, Class<T> dataClass, Consumer<ScheduledExecution<T>> consumer)Gets all scheduled executions for a task and supplies them to the provided Consumer.List<CurrentlyExecuting>getCurrentlyExecuting()List<CurrentlyExecuting>getCurrentlyExecutingWithStaleHeartbeat()List<Execution>getFailingExecutions(Duration failingAtLeastFor)Optional<ScheduledExecution<Object>>getScheduledExecution(TaskInstanceId taskInstanceId)Gets the details for a specific scheduled execution.SchedulerStategetSchedulerState()voidpause()voidregisterSchedulerListener(SchedulerListener listener)<T> booleanreschedule(SchedulableInstance<T> schedulableInstance)Update an existing execution with a new execution-time and new task-data.booleanreschedule(TaskInstanceId taskInstanceId, Instant newExecutionTime)Update an existing execution to a new execution-time.<T> booleanreschedule(TaskInstanceId taskInstanceId, Instant newExecutionTime, T newData)Update an existing execution with a new execution-time and new task-data.voidresume()<T> voidschedule(SchedulableInstance<T> schedulableInstance)<T> booleanschedule(SchedulableInstance<T> schedulableInstance, SchedulerClient.ScheduleOptions scheduleOptions)Schedule a new execution for the given task instance.<T> voidschedule(TaskInstance<T> taskInstance, Instant executionTime)Schedule a new execution if task instance does not already exists.<T> booleanschedule(TaskInstance<T> taskInstance, Instant executionTime, SchedulerClient.ScheduleOptions scheduleOptions)Schedule a new execution for the given task instance.voidscheduleBatch(List<SchedulableInstance<?>> schedulableInstances)Schedule a batch of executions.voidscheduleBatch(List<TaskInstance<?>> taskInstances, Instant executionTime)Schedule a batch of executions.<T> booleanscheduleIfNotExists(SchedulableInstance<T> schedulableInstance)Schedule a new execution if task instance does not already exists.<T> booleanscheduleIfNotExists(TaskInstance<T> taskInstance, Instant executionTime)Schedule a new execution if task instance does not already exists.voidstart()voidstop()voidtriggerCheckForDueExecutions()protected voidupdateHeartbeatForExecution(Instant now, CurrentlyExecuting currentlyExecuting)-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.github.kagkarlsson.scheduler.SchedulerClient
getScheduledExecutions, getScheduledExecutions, getScheduledExecutionsForTask, getScheduledExecutionsForTask, getScheduledExecutionsForTask, scheduleBatch, scheduleBatch
-
-
-
-
Field Detail
-
TRIGGER_NEXT_BATCH_WHEN_AVAILABLE_THREADS_RATIO
public static final double TRIGGER_NEXT_BATCH_WHEN_AVAILABLE_THREADS_RATIO
- See Also:
- Constant Field Values
-
THREAD_PREFIX
public static final String THREAD_PREFIX
- See Also:
- Constant Field Values
-
executeDueStrategy
protected final PollStrategy executeDueStrategy
-
executor
protected final Executor executor
-
-
Constructor Detail
-
Scheduler
protected Scheduler(Clock clock, TaskRepository schedulerTaskRepository, TaskRepository clientTaskRepository, TaskResolver taskResolver, int threadpoolSize, ExecutorService executorService, SchedulerName schedulerName, Waiter executeDueWaiter, Duration heartbeatInterval, int numberOfMissedHeartbeatsBeforeDead, List<SchedulerListener> schedulerListeners, List<ExecutionInterceptor> executionInterceptors, PollingStrategyConfig pollingStrategyConfig, Duration deleteUnresolvedAfter, Duration shutdownMaxWait, LogLevel logLevel, boolean logStackTrace, List<OnStartup> onStartup, ExecutorService dueExecutor, ScheduledExecutorService housekeeperExecutor)
-
-
Method Detail
-
registerSchedulerListener
public void registerSchedulerListener(SchedulerListener listener)
-
start
public void start()
-
executeDue
protected void executeDue()
-
executeOnStartup
protected void executeOnStartup()
-
stop
public void stop()
-
pause
public void pause()
-
resume
public void resume()
-
getSchedulerState
public SchedulerState getSchedulerState()
-
schedule
public <T> void schedule(SchedulableInstance<T> schedulableInstance)
- Specified by:
schedulein interfaceSchedulerClient
-
scheduleIfNotExists
public <T> boolean scheduleIfNotExists(TaskInstance<T> taskInstance, Instant executionTime)
Description copied from interface:SchedulerClientSchedule a new execution if task instance does not already exists.- Specified by:
scheduleIfNotExistsin interfaceSchedulerClient- Parameters:
taskInstance- Task-instance, optionally with dataexecutionTime- Instant it should run- Returns:
- true if scheduled successfully
- See Also:
Instant,TaskInstance
-
scheduleIfNotExists
public <T> boolean scheduleIfNotExists(SchedulableInstance<T> schedulableInstance)
Description copied from interface:SchedulerClientSchedule a new execution if task instance does not already exists.- Specified by:
scheduleIfNotExistsin interfaceSchedulerClient- Parameters:
schedulableInstance- Task-instance and time it should run- Returns:
- true if scheduled successfully
- See Also:
SchedulableInstance
-
scheduleBatch
public void scheduleBatch(List<TaskInstance<?>> taskInstances, Instant executionTime)
Description copied from interface:SchedulerClientSchedule a batch of executions. If any of the executions already exists, the scheduling will fail and an exception will be thrown.- Specified by:
scheduleBatchin interfaceSchedulerClient- Parameters:
taskInstances- Task-instance to schedule, optionally with dataexecutionTime- Instant it should run- See Also:
Instant,TaskInstance
-
scheduleBatch
public void scheduleBatch(List<SchedulableInstance<?>> schedulableInstances)
Description copied from interface:SchedulerClientSchedule a batch of executions. If any of the executions already exists, the scheduling will fail and an exception will be thrown.- Specified by:
scheduleBatchin interfaceSchedulerClient- Parameters:
schedulableInstances- Task-instances with individual execution-times- See Also:
SchedulableInstance
-
schedule
public <T> void schedule(TaskInstance<T> taskInstance, Instant executionTime)
Description copied from interface:SchedulerClientSchedule a new execution if task instance does not already exists.- Specified by:
schedulein interfaceSchedulerClient- Parameters:
taskInstance- Task-instance, optionally with dataexecutionTime- Instant it should run- See Also:
Instant,TaskInstance
-
reschedule
public boolean reschedule(TaskInstanceId taskInstanceId, Instant newExecutionTime)
Description copied from interface:SchedulerClientUpdate an existing execution to a new execution-time. If the execution does not exist or if it is currently running, an exception is thrown.- Specified by:
reschedulein interfaceSchedulerClient- Parameters:
taskInstanceId- Task-instance to reschedule, expected to existnewExecutionTime- the new execution-time- Returns:
- true if rescheduled successfully
- See Also:
Instant,TaskInstanceId
-
reschedule
public <T> boolean reschedule(SchedulableInstance<T> schedulableInstance)
Description copied from interface:SchedulerClientUpdate an existing execution with a new execution-time and new task-data. If the execution does not exist or if it is currently running, an exception is thrown.- Specified by:
reschedulein interfaceSchedulerClient- Parameters:
schedulableInstance- the updated instance- Returns:
- true if rescheduled successfully
-
reschedule
public <T> boolean reschedule(TaskInstanceId taskInstanceId, Instant newExecutionTime, T newData)
Description copied from interface:SchedulerClientUpdate an existing execution with a new execution-time and new task-data. If the execution does not exist or if it is currently running, an exception is thrown.- Specified by:
reschedulein interfaceSchedulerClientnewExecutionTime- the new execution-timenewData- the new task-data- Returns:
- true if rescheduled successfully
- See Also:
Instant,TaskInstanceId
-
schedule
public <T> boolean schedule(TaskInstance<T> taskInstance, Instant executionTime, SchedulerClient.ScheduleOptions scheduleOptions)
Description copied from interface:SchedulerClientSchedule a new execution for the given task instance.If the task instance already exists, the specified policy in
scheduleOptionsapplies.An exception is thrown if the execution is currently running.
- Specified by:
schedulein interfaceSchedulerClient- Parameters:
taskInstance- Task-instance, optionally with dataexecutionTime- Instant it should runscheduleOptions- policy for when the instance exists- Returns:
- true if the task-instance actually was scheduled
- See Also:
Instant,TaskInstance,TaskInstanceCurrentlyExecutingException
-
schedule
public <T> boolean schedule(SchedulableInstance<T> schedulableInstance, SchedulerClient.ScheduleOptions scheduleOptions)
Description copied from interface:SchedulerClientSchedule a new execution for the given task instance.If the task instance already exists, the specified policy in
scheduleOptionsapplies.An exception is thrown if the execution is currently running.
- Specified by:
schedulein interfaceSchedulerClient- Parameters:
schedulableInstance- Task-instance and time it should runscheduleOptions- policy for when the instance exists- Returns:
- true if the task-instance actually was scheduled
- See Also:
SchedulableInstance,TaskInstanceCurrentlyExecutingException
-
cancel
public void cancel(TaskInstanceId taskInstanceId)
Description copied from interface:SchedulerClientRemoves/Cancels an execution.- Specified by:
cancelin interfaceSchedulerClient- See Also:
TaskInstanceId
-
fetchScheduledExecutions
public void fetchScheduledExecutions(Consumer<ScheduledExecution<Object>> consumer)
Description copied from interface:SchedulerClientGets all scheduled executions and supplies them to the provided Consumer. A Consumer is used to avoid forcing the SchedulerClient to load all executions in memory. Currently running executions are not returned.- Specified by:
fetchScheduledExecutionsin interfaceSchedulerClient- Parameters:
consumer- Consumer for the executions
-
fetchScheduledExecutions
public void fetchScheduledExecutions(ScheduledExecutionsFilter filter, Consumer<ScheduledExecution<Object>> consumer)
- Specified by:
fetchScheduledExecutionsin interfaceSchedulerClient
-
fetchScheduledExecutionsForTask
public <T> void fetchScheduledExecutionsForTask(String taskName, Class<T> dataClass, Consumer<ScheduledExecution<T>> consumer)
Description copied from interface:SchedulerClientGets all scheduled executions for a task and supplies them to the provided Consumer. A Consumer is used to avoid forcing the SchedulerClient to load all executions in memory. Currently running executions are not returned.- Specified by:
fetchScheduledExecutionsForTaskin interfaceSchedulerClient- Parameters:
taskName- the name of the task to get scheduled-executions fordataClass- the task data-class the data will be serialized and cast toconsumer- Consumer for the executions
-
fetchScheduledExecutionsForTask
public <T> void fetchScheduledExecutionsForTask(String taskName, Class<T> dataClass, ScheduledExecutionsFilter filter, Consumer<ScheduledExecution<T>> consumer)
- Specified by:
fetchScheduledExecutionsForTaskin interfaceSchedulerClient
-
getScheduledExecution
public Optional<ScheduledExecution<Object>> getScheduledExecution(TaskInstanceId taskInstanceId)
Description copied from interface:SchedulerClientGets the details for a specific scheduled execution. Currently running executions are also returned.- Specified by:
getScheduledExecutionin interfaceSchedulerClient- Returns:
- Optional.empty() if no matching execution found
- See Also:
TaskInstanceId,ScheduledExecution
-
triggerCheckForDueExecutions
public void triggerCheckForDueExecutions()
-
getCurrentlyExecuting
public List<CurrentlyExecuting> getCurrentlyExecuting()
-
getCurrentlyExecutingWithStaleHeartbeat
public List<CurrentlyExecuting> getCurrentlyExecutingWithStaleHeartbeat()
-
detectDeadExecutions
protected void detectDeadExecutions()
-
updateHeartbeatForExecution
protected void updateHeartbeatForExecution(Instant now, CurrentlyExecuting currentlyExecuting)
-
create
public static SchedulerBuilder create(DataSource dataSource, Task<?>... knownTasks)
-
create
public static SchedulerBuilder create(DataSource dataSource, List<Task<?>> knownTasks)
-
-