Interface Scheduler

  • All Known Implementing Classes:
    ThreadPool

    public interface Scheduler
    Scheduler that allows to schedule one-shot and periodic commands.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Interface Description
      static interface  Scheduler.Cancellable
      This interface represents an object whose execution may be cancelled during runtime.
      static class  Scheduler.ReschedulingRunnable
      This class encapsulates the scheduling of a Runnable that needs to be repeated on a interval.
      static class  Scheduler.SafeScheduledThreadPoolExecutor
      This subclass ensures to properly bubble up Throwable instances of both type Error and Exception thrown in submitted/scheduled tasks to the uncaught exception handler
      static interface  Scheduler.ScheduledCancellable
      A scheduled cancellable allow cancelling and reading the remaining delay of a scheduled task.
    • Method Summary

      Modifier and Type Method Description
      static boolean awaitTermination​(java.util.concurrent.ScheduledThreadPoolExecutor scheduledThreadPoolExecutor, long timeout, java.util.concurrent.TimeUnit timeUnit)  
      static java.util.concurrent.ScheduledThreadPoolExecutor initScheduler​(Settings settings)
      Create a scheduler that can be used client side.
      default java.lang.Runnable preserveContext​(java.lang.Runnable command)
      Does nothing by default but can be used by subclasses to save the current thread context and wraps the command in a Runnable that restores that context before running the command.
      Scheduler.ScheduledCancellable schedule​(java.lang.Runnable command, TimeValue delay, java.lang.String executor)
      Schedules a one-shot command to be run after a given delay.
      default java.util.concurrent.ScheduledFuture<?> schedule​(TimeValue delay, java.lang.String executor, java.lang.Runnable command)
      Deprecated.
      default Scheduler.Cancellable scheduleWithFixedDelay​(java.lang.Runnable command, TimeValue interval, java.lang.String executor)
      Schedules a periodic action that runs on scheduler thread.
      static boolean terminate​(java.util.concurrent.ScheduledThreadPoolExecutor scheduledThreadPoolExecutor, long timeout, java.util.concurrent.TimeUnit timeUnit)  
      static Scheduler.Cancellable wrapAsCancellable​(java.util.concurrent.Future<?> future)
      Utility method to wrap a Future as a Cancellable
      static Scheduler.ScheduledCancellable wrapAsScheduledCancellable​(java.util.concurrent.ScheduledFuture<?> scheduledFuture)
      Utility method to wrap a ScheduledFuture as a ScheduledCancellable
    • Method Detail

      • initScheduler

        static java.util.concurrent.ScheduledThreadPoolExecutor initScheduler​(Settings settings)
        Create a scheduler that can be used client side. Server side, please use ThreadPool.schedule instead. Notice that if any scheduled jobs fail with an exception, these will bubble up to the uncaught exception handler where they will be logged as a warning. This includes jobs started using execute, submit and schedule.
        Parameters:
        settings - the settings to use
        Returns:
        executor
      • terminate

        static boolean terminate​(java.util.concurrent.ScheduledThreadPoolExecutor scheduledThreadPoolExecutor,
                                 long timeout,
                                 java.util.concurrent.TimeUnit timeUnit)
      • awaitTermination

        static boolean awaitTermination​(java.util.concurrent.ScheduledThreadPoolExecutor scheduledThreadPoolExecutor,
                                        long timeout,
                                        java.util.concurrent.TimeUnit timeUnit)
      • preserveContext

        default java.lang.Runnable preserveContext​(java.lang.Runnable command)
        Does nothing by default but can be used by subclasses to save the current thread context and wraps the command in a Runnable that restores that context before running the command.
      • schedule

        Scheduler.ScheduledCancellable schedule​(java.lang.Runnable command,
                                                TimeValue delay,
                                                java.lang.String executor)
        Schedules a one-shot command to be run after a given delay. The command is not run in the context of the calling thread. To preserve the context of the calling thread you may call preserveContext(Runnable) on the runnable before passing it to this method. The command runs on scheduler thread. Do not run blocking calls on the scheduler thread. Subclasses may allow to execute on a different executor, in which case blocking calls are allowed.
        Parameters:
        command - the command to run
        delay - delay before the task executes
        executor - the name of the executor that has to execute this task. Ignored in the default implementation but can be used by subclasses that support multiple executors.
        Returns:
        a ScheduledFuture who's get will return when the task has been added to its target thread pool and throws an exception if the task is canceled before it was added to its target thread pool. Once the task has been added to its target thread pool the ScheduledFuture cannot interact with it.
        Throws:
        EsRejectedExecutionException - if the task cannot be scheduled for execution
      • schedule

        @Deprecated
        default java.util.concurrent.ScheduledFuture<?> schedule​(TimeValue delay,
                                                                 java.lang.String executor,
                                                                 java.lang.Runnable command)
        Deprecated.
        Schedules a one-shot command to be run after a given delay. The command is not run in the context of the calling thread. To preserve the context of the calling thread you may call preserveContext(Runnable) on the runnable before passing it to this method. The command runs on scheduler thread. Do not run blocking calls on the scheduler thread. Subclasses may allow to execute on a different executor, in which case blocking calls are allowed.
        Parameters:
        delay - delay before the task executes
        executor - the name of the executor that has to execute this task. Ignored in the default implementation but can be used by subclasses that support multiple executors.
        command - the command to run
        Returns:
        a ScheduledFuture who's get will return when the task has been added to its target thread pool and throws an exception if the task is canceled before it was added to its target thread pool. Once the task has been added to its target thread pool the ScheduledFuture cannot interact with it.
        Throws:
        EsRejectedExecutionException - if the task cannot be scheduled for execution
      • scheduleWithFixedDelay

        default Scheduler.Cancellable scheduleWithFixedDelay​(java.lang.Runnable command,
                                                             TimeValue interval,
                                                             java.lang.String executor)
        Schedules a periodic action that runs on scheduler thread. Do not run blocking calls on the scheduler thread. Subclasses may allow to execute on a different executor, in which case blocking calls are allowed.
        Parameters:
        command - the action to take
        interval - the delay interval
        executor - the name of the executor that has to execute this task. Ignored in the default implementation but can be used by subclasses that support multiple executors.
        Returns:
        a Scheduler.Cancellable that can be used to cancel the subsequent runs of the command. If the command is running, it will not be interrupted.
      • wrapAsCancellable

        static Scheduler.Cancellable wrapAsCancellable​(java.util.concurrent.Future<?> future)
        Utility method to wrap a Future as a Cancellable
        Parameters:
        future - the future to wrap
        Returns:
        a cancellable delegating to the future
      • wrapAsScheduledCancellable

        static Scheduler.ScheduledCancellable wrapAsScheduledCancellable​(java.util.concurrent.ScheduledFuture<?> scheduledFuture)
        Utility method to wrap a ScheduledFuture as a ScheduledCancellable
        Parameters:
        scheduledFuture - the scheduled future to wrap
        Returns:
        a SchedulecCancellable delegating to the scheduledFuture