Class BackgroundTask

java.lang.Object
org.apache.camel.support.task.BackgroundTask
All Implemented Interfaces:
BlockingTask, Task

public class BackgroundTask extends Object implements BlockingTask
A sleepless blocking task that runs in a thread in the background (using a scheduled thread pool). The execution is processed until the task budget is either completed, or exhausted. All background tasks are constrained by a time budget.
  • Method Details

    • schedule

      public Future<?> schedule(org.apache.camel.CamelContext camelContext, BooleanSupplier supplier)
      Schedules the task to be run
      Parameters:
      camelContext - the camel context
      supplier - the task as a boolean supplier. The result is used to check if the task has completed or not. The supplier must return true if the execution has completed or false otherwise.
      Returns:
      a future for the task
    • cancel

      public void cancel(boolean mayInterruptIfRunning)
      Cancels a task scheduled with schedule(CamelContext, BooleanSupplier) that is no longer needed, and removes it from the TaskManagerRegistry. A scheduled task deregisters itself from one of its runs, which is not going to happen once the schedule is cancelled, so cancelling the returned Future directly leaves the task behind in the registry.

      This does not wait for an attempt that is already running: with mayInterruptIfRunning false, a supplier call that is in progress runs to completion after this method returns. isRunning() answers for the schedule and turns false here even then, so isAttempting() is the one to ask whether an attempt is still in flight.

      A task that already completed, failed or exhausted its budget keeps the outcome of its last run. Only the schedule of a task that is still Task.Status.Active is cancelled, which turns it Task.Status.Inactive.

      Parameters:
      mayInterruptIfRunning - whether the thread of an attempt that is currently running should be interrupted
    • run

      public boolean run(org.apache.camel.CamelContext camelContext, BooleanSupplier supplier)
      Description copied from interface: BlockingTask
      Run the task
      Specified by:
      run in interface BlockingTask
      Parameters:
      camelContext - the camel context
      supplier - the task as a boolean supplier. The result is used to check if the task has completed or not. The supplier must return true if the execution has completed or false otherwise.
      Returns:
      true if the task has completed successfully or false if: 1) the budget is exhausted or 2) the task was interrupted.
    • isAttempting

      public boolean isAttempting()
      Description copied from interface: Task
      Whether the task is currently attempting to run its supplier (true), or waiting for the next scheduled tick (false).
      Specified by:
      isAttempting in interface Task
    • doRun

      protected boolean doRun(BooleanSupplier supplier)
    • isRunning

      public boolean isRunning()
      Description copied from interface: BlockingTask
      Whether the task has been submitted for running (the state of the task can be waiting for next run etc).
      Specified by:
      isRunning in interface BlockingTask
      Returns:
      true if the run method has been called.
    • elapsed

      public Duration elapsed()
      Description copied from interface: Task
      How long it took to run the task when the task was completed
      Specified by:
      elapsed in interface Task
      Returns:
      The duration to execute the task
    • iteration

      public int iteration()
      Description copied from interface: Task
      The current number of iterations (such as when the task is being repeated)
      Specified by:
      iteration in interface Task
      Returns:
      the current number of iterations
    • getCurrentDelay

      public long getCurrentDelay()
      Description copied from interface: Task
      The current computed delay.
      Specified by:
      getCurrentDelay in interface Task
    • getName

      public String getName()
      Description copied from interface: Task
      Optional name of the task
      Specified by:
      getName in interface Task
    • getStatus

      public Task.Status getStatus()
      Description copied from interface: Task
      Gets the task status.
      Specified by:
      getStatus in interface Task
    • getFirstAttemptTime

      public long getFirstAttemptTime()
      Description copied from interface: Task
      The time the first attempt was performed.
      Specified by:
      getFirstAttemptTime in interface Task
    • getLastAttemptTime

      public long getLastAttemptTime()
      Description copied from interface: Task
      The time the last attempt has been performed.
      Specified by:
      getLastAttemptTime in interface Task
    • getNextAttemptTime

      public long getNextAttemptTime()
      Description copied from interface: Task
      An indication about the time the next attempt will be made.
      Specified by:
      getNextAttemptTime in interface Task
    • getCurrentElapsedTime

      public long getCurrentElapsedTime()
      Description copied from interface: Task
      The current elapsed time.
      Specified by:
      getCurrentElapsedTime in interface Task
    • getException

      public Throwable getException()
      Description copied from interface: Task
      The task failed for some un-expected exception
      Specified by:
      getException in interface Task