Class SubmitterSchedulerTaskInterceptor

    • Constructor Summary

      Constructors 
      Constructor Description
      SubmitterSchedulerTaskInterceptor​(SubmitterScheduler parentScheduler, java.util.function.BiFunction<java.lang.Runnable,​java.lang.Boolean,​java.lang.Runnable> taskManipulator)
      Constructs a wrapper for SubmitterScheduler pool so that tasks can be intercepted and modified, before being submitted to the pool.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void schedule​(java.lang.Runnable task, long delayInMs)
      Schedule a one time task with a given delay.
      void scheduleAtFixedRate​(java.lang.Runnable task, long initialDelay, long period)
      Schedule a fixed rate recurring task to run.
      void scheduleWithFixedDelay​(java.lang.Runnable task, long initialDelay, long recurringDelay)
      Schedule a fixed delay recurring task to run.
      <T> ListenableFuture<T> submitScheduled​(java.lang.Runnable task, T result, long delayInMs)
      Schedule a task with a given delay.
      <T> ListenableFuture<T> submitScheduled​(java.util.concurrent.Callable<T> task, long delayInMs)
      Schedule a Callable with a given delay.
      java.lang.Runnable wrapTask​(java.lang.Runnable task)
      Overridden version which delegates to wrapTask(Runnable, boolean).
      java.lang.Runnable wrapTask​(java.lang.Runnable task, boolean recurring)
      Implementation to modify a provided task.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.concurrent.Executor

        execute
    • Constructor Detail

      • SubmitterSchedulerTaskInterceptor

        public SubmitterSchedulerTaskInterceptor​(SubmitterScheduler parentScheduler,
                                                 java.util.function.BiFunction<java.lang.Runnable,​java.lang.Boolean,​java.lang.Runnable> taskManipulator)
        Constructs a wrapper for SubmitterScheduler pool so that tasks can be intercepted and modified, before being submitted to the pool.
        Parameters:
        parentScheduler - An instance of SubmitterScheduler to wrap
        taskManipulator - A lambda to manipulate a Runnable that was submitted for execution
    • Method Detail

      • wrapTask

        public final java.lang.Runnable wrapTask​(java.lang.Runnable task)
        Overridden version which delegates to wrapTask(Runnable, boolean). There should be no reason to override this, instead just ensure that wrapTask(Runnable, boolean) is implemented.
        Overrides:
        wrapTask in class ExecutorTaskInterceptor
        Parameters:
        task - A runnable that was submitted for execution
        Returns:
        A non-null task that will be provided to the parent executor
      • wrapTask

        public java.lang.Runnable wrapTask​(java.lang.Runnable task,
                                           boolean recurring)
        Implementation to modify a provided task. The provided runnable will be the one submitted to the Executor, unless a Callable was submitted, in which case a ListenableFutureTask will be provided. In the last condition the original callable can be retrieved by invoking ListenableFutureTask.getContainedCallable(). The returned task can not be null, but could be either the original task, a modified task, a wrapper to the provided task, or if no action is desired DoNothingRunnable.instance() may be provided. However caution should be used in that if a ListenableFutureTask is provided, and then never returned (and not canceled), then the future will never complete (and thus possibly forever blocked). So if you are doing conditional checks for ListenableFutureTask and may not execute/return the provided task, then you should be careful to ensure Future.cancel(boolean) is invoked.

        Public visibility for javadoc visibility.

        Parameters:
        task - A runnable that was submitted for execution
        recurring - true if the provided task is a recurring task
        Returns:
        A non-null task that will be provided to the parent executor
      • schedule

        public void schedule​(java.lang.Runnable task,
                             long delayInMs)
        Description copied from interface: SubmitterScheduler
        Schedule a one time task with a given delay.
        Specified by:
        schedule in interface SubmitterScheduler
        Parameters:
        task - runnable to execute
        delayInMs - time in milliseconds to wait to execute task
      • submitScheduled

        public <T> ListenableFuture<T> submitScheduled​(java.lang.Runnable task,
                                                       T result,
                                                       long delayInMs)
        Description copied from interface: SubmitterScheduler
        Schedule a task with a given delay. The Future.get() method will return the provided result once the runnable has completed.
        Specified by:
        submitScheduled in interface SubmitterScheduler
        Type Parameters:
        T - type of result returned from the future
        Parameters:
        task - runnable to execute
        result - result to be returned from resulting future .get() when runnable completes
        delayInMs - time in milliseconds to wait to execute task
        Returns:
        a future to know when the task has completed
      • submitScheduled

        public <T> ListenableFuture<T> submitScheduled​(java.util.concurrent.Callable<T> task,
                                                       long delayInMs)
        Description copied from interface: SubmitterScheduler
        Schedule a Callable with a given delay. This is needed when a result needs to be consumed from the callable.
        Specified by:
        submitScheduled in interface SubmitterScheduler
        Type Parameters:
        T - type of result returned from the future
        Parameters:
        task - callable to be executed
        delayInMs - time in milliseconds to wait to execute task
        Returns:
        a future to know when the task has completed and get the result of the callable
      • scheduleWithFixedDelay

        public void scheduleWithFixedDelay​(java.lang.Runnable task,
                                           long initialDelay,
                                           long recurringDelay)
        Description copied from interface: SubmitterScheduler
        Schedule a fixed delay recurring task to run. The recurring delay time will be from the point where execution has finished. So the execution frequency is the recurringDelay + runtime for the provided task.

        Unlike ScheduledExecutorService if the task throws an exception, subsequent executions are NOT suppressed or prevented. So if the task throws an exception on every run, the task will continue to be executed at the provided recurring delay (possibly throwing an exception on each execution).

        Specified by:
        scheduleWithFixedDelay in interface SubmitterScheduler
        Parameters:
        task - runnable to be executed
        initialDelay - delay in milliseconds until first run
        recurringDelay - delay in milliseconds for running task after last finish
      • scheduleAtFixedRate

        public void scheduleAtFixedRate​(java.lang.Runnable task,
                                        long initialDelay,
                                        long period)
        Description copied from interface: SubmitterScheduler
        Schedule a fixed rate recurring task to run. The recurring delay will be the same, regardless of how long task execution takes. A given runnable will not run concurrently (unless it is submitted to the scheduler multiple times). Instead of execution takes longer than the period, the next run will occur immediately (given thread availability in the pool).

        Unlike ScheduledExecutorService if the task throws an exception, subsequent executions are NOT suppressed or prevented. So if the task throws an exception on every run, the task will continue to be executed at the provided recurring delay (possibly throwing an exception on each execution).

        Specified by:
        scheduleAtFixedRate in interface SubmitterScheduler
        Parameters:
        task - runnable to be executed
        initialDelay - delay in milliseconds until first run
        period - amount of time in milliseconds between the start of recurring executions