Class AbstractTask

  • All Implemented Interfaces:
    Task

    public abstract class AbstractTask
    extends java.lang.Object
    implements Task
    This abstract implementation of Task handles some of the complexity of dealing with pausing, cancelling, and resuming. Implementations only have to implement doTaskWork(), though they may also implement the state related functions to allow for pausing.
    • Constructor Detail

      • AbstractTask

        public AbstractTask​(java.util.UUID id)
    • Method Detail

      • getId

        public java.util.UUID getId()
        Specified by:
        getId in interface Task
      • getLoggerName

        protected abstract java.lang.String getLoggerName()
      • getLogger

        protected LoggerEx getLogger()
      • getCurrentContext

        protected TaskContext getCurrentContext()
      • isStopped

        protected boolean isStopped()
      • run

        public final void run​(TaskContext context)
                       throws java.lang.Exception
        Description copied from interface: Task
        Executes the task. This function should block until completion. If there is a problem executing the task, an exception should be thrown. It is important to note that while this function should block, the task must expect that at some point, suspend or cancel may be called.
        Specified by:
        run in interface Task
        Throws:
        java.lang.Exception
      • suspend

        public java.util.Optional<Task.TaskStateModel> suspend​(long timeoutMS)
                                                        throws java.util.concurrent.TimeoutException,
                                                               java.lang.InterruptedException
        Description copied from interface: Task
        When called, the task should stop as quickly as possible. If the task is able to resume, it should return a state that will be provided to it when restarted. If this function returns an empty value, it will not be resumed. If appropriate, the call should try to implement the timeout restriction passed in. A timeout of 0 means "no timeout".
        Specified by:
        suspend in interface Task
        Throws:
        java.util.concurrent.TimeoutException
        java.lang.InterruptedException
      • cancel

        public void cancel​(long timeoutMS)
                    throws java.util.concurrent.TimeoutException,
                           java.lang.InterruptedException
        Description copied from interface: Task
        When called, the task should stop as quickly as possible. If appropriate, the call should try to implement the timeout restriction passed in. A timeout of 0 means "no timeout".
        Specified by:
        cancel in interface Task
        Throws:
        java.util.concurrent.TimeoutException
        java.lang.InterruptedException
      • getTaskStateModel

        protected java.util.Optional<Task.TaskStateModel> getTaskStateModel()
        Can be overridded by sub tasks to provide a state model when the task is suspended. This will be called after doTaskWork() has exited when the task is "stopped"
      • applyTaskStateModel

        protected void applyTaskStateModel​(Task.TaskStateModel model)
        Can be overridden by sub classes to receive the state model before the task is resumed. At some point soon after this is called, the doTaskWork() function will be called.
      • doTaskWork

        protected abstract void doTaskWork​(TaskContext context)
                                    throws java.lang.Exception
        Should execute the work of the task. It is possible that this function will be called multiple times in the lifecycle of the task. The task may be paused, or the task may choose to yield execution. To yield, it should set its state to "WAITING", and return out of this function. At some point later, it can be resumed by calling resumeTask() on the context, or by the task manager.

        Inside of this function, the task should periodically check isStopped to see if the task has been paused or canceled.

        Throws:
        java.lang.Exception