Class AbstractTask<T>

java.lang.Object
org.jppf.node.protocol.AbstractTask<T>
Type Parameters:
T - the type of results returned by this task.
All Implemented Interfaces:
Serializable, Runnable, Interruptibility, PositionalElement<Task<T>>, Task<T>
Direct Known Subclasses:
AbstractTaskNode, CommandLineTask, CrossoverTask, FitnessTask, JPPFAnnotatedTask, JPPFExceptionResult, JPPFTask, ScriptedTask

public class AbstractTask<T> extends Object implements Task<T>
Abstract superclass for all tasks submitted to the execution server. This class provides the basic facilities to handle data shared among tasks, handling of task execution exception, and handling of the execution results.

JPPF clients have to extend this class and must implement the run method. In the run method the task calculations are performed, and the result of the calculations is set with the setResult(Object) method:

 class MyTask extends AbstractTask<Object> {
   public void run() {
     // do the calculation ...
     setResult(myResult);
   }
 }
 
Since:
4.0
Author:
Laurent Cohen
See Also:
  • Constructor Details

    • AbstractTask

      public AbstractTask()
  • Method Details

    • getResult

      public T getResult()
      Description copied from interface: Task
      Get the result of the task execution.
      Specified by:
      getResult in interface Task<T>
      Returns:
      the task result.
    • setResult

      public Task<T> setResult(T result)
      Description copied from interface: Task
      Set the result of the task execution.
      Specified by:
      setResult in interface Task<T>
      Parameters:
      result - the result of this task's execution.
      Returns:
      this task, for method chaining.
    • getThrowable

      public Throwable getThrowable()
      Description copied from interface: Task
      Get the Throwable that was raised by this task's execution.
      Specified by:
      getThrowable in interface Task<T>
      Returns:
      a Throwable instance, or null if no exception was raised.
    • setThrowable

      public Task<T> setThrowable(Throwable throwable)
      Description copied from interface: Task
      Sets the Throwable that was raised by this task's execution in the run() method. The Throwable is normally set by the JPPF framework.
      Specified by:
      setThrowable in interface Task<T>
      Parameters:
      throwable - a Throwable instance.
      Returns:
      this task, for method chaining.
    • getDataProvider

      public DataProvider getDataProvider()
      Description copied from interface: Task
      Get the provider of shared data for this task.
      Specified by:
      getDataProvider in interface Task<T>
      Returns:
      a DataProvider instance.
    • setDataProvider

      public Task<T> setDataProvider(DataProvider dataProvider)
      Set the provider of shared data for this task.
      Specified by:
      setDataProvider in interface Task<T>
      Parameters:
      dataProvider - a DataProvider instance.
      Returns:
      this task, for method chaining.
    • getId

      public String getId()
      Description copied from interface: Task
      Get the user-assigned id for this task.
      Specified by:
      getId in interface Task<T>
      Returns:
      the id as a string.
    • setId

      public Task<T> setId(String id)
      Description copied from interface: Task
      Set the user-assigned id for this task.
      Specified by:
      setId in interface Task<T>
      Parameters:
      id - the id as a string.
      Returns:
      this task, for method chaining.
    • onCancel

      public void onCancel()
      Description copied from interface: Task
      Callback invoked when this task is cancelled. The default implementation does nothing and should be overriden by subclasses that desire to implement a specific behaviour on cancellation.
      Specified by:
      onCancel in interface Task<T>
    • onTimeout

      public void onTimeout()
      Description copied from interface: Task
      Callback invoked when this task times out. The default implementation does nothing and should be overriden by subclasses that desire to implement a specific behaviour on timeout.
      Specified by:
      onTimeout in interface Task<T>
    • getTaskObject

      public Object getTaskObject()
      Description copied from interface: Task
      Get the JPPFRunnable-annotated object or POJO wrapped by this task.
      Specified by:
      getTaskObject in interface Task<T>
      Returns:
      an object or class that is JPPF-annotated.
    • getTimeoutSchedule

      public JPPFSchedule getTimeoutSchedule()
      Description copied from interface: Task
      Get the task timeout schedule configuration.
      Specified by:
      getTimeoutSchedule in interface Task<T>
      Returns:
      a JPPFSchedule instance.
    • setTimeoutSchedule

      public Task<T> setTimeoutSchedule(JPPFSchedule timeoutSchedule)
      Description copied from interface: Task
      Get the task timeout schedule configuration.
      Specified by:
      setTimeoutSchedule in interface Task<T>
      Parameters:
      timeoutSchedule - a JPPFSchedule instance.
      Returns:
      this task, for method chaining.
    • getPosition

      public int getPosition()
      Returns the position of this element in its container.
      Specified by:
      getPosition in interface PositionalElement<T>
      Returns:
      the position of this element as an int.
    • setPosition

      public Task<T> setPosition(int position)
      Set the position of this element in its container.
      Specified by:
      setPosition in interface PositionalElement<T>
      Parameters:
      position - the position of this task as an int.
      Returns:
      this element, for method call chaining.
    • isInNode

      public boolean isInNode()
      Description copied from interface: Task
      Determine whether this task is executing within a node, or locally on the client side.
      Specified by:
      isInNode in interface Task<T>
      Returns:
      true if this task is executing in a node, false if it is on the client side.
    • setInNode

      public Task<T> setInNode(boolean inNode)
      Determine whether this task is executing within a node, or locally on the client side.
      Specified by:
      setInNode in interface Task<T>
      Parameters:
      inNode - true if this task is executing in a node, false if it is on the client side.
      Returns:
      this task, for method chaining.
    • compute

      public <V> V compute(JPPFCallable<V> callable) throws Exception
      Description copied from interface: Task
      Compute a value on the client-side, as the result of the execution of a JPPFCallable.

      Any Throwable raised in the callable's call() method will be thrown as the result of this method. If the Throwable is an instance of Exception or one of its subclasses, it is thrown as such, otherwise it is wrapped into a JPPFException.

      Specified by:
      compute in interface Task<T>
      Type Parameters:
      V - the type of results returned by the callable.
      Parameters:
      callable - the callable to execute on the client side.
      Returns:
      the value computed on the client, or null if the value could not be computed.
      Throws:
      Exception - if the execution of the callable in the client resulted in a Throwable being raised.
    • fireNotification

      public Task<T> fireNotification(Object userObject, boolean sendViaJmx)
      Description copied from interface: Task
      Causes the task to send a notification to all listeners. This method can be called at any time during the execution of the task, i.e. while in the execution scope of the Runnable.run(), Task.onTimeout() or Task.onCancel() methods.

      If the parameter sendViaJmx is true, then a notfication will also be sent by the JPPFNodeTaskMonitorMBean mbean, otherwise only local listeners will be notified.

      Specified by:
      fireNotification in interface Task<T>
      Parameters:
      userObject - a user-defined object to send as part of the notification.
      sendViaJmx - if true then also send the notification via the JMX MBean, otherwise only send to local listeners. If the task is executing within a client local executor, this parameter has no effect.
      Returns:
      this task, for method chaining.
    • setExecutionDispatcher

      public void setExecutionDispatcher(TaskExecutionDispatcher executionDisptacher)
      Set the task notification dispatcher onto this task.
      Parameters:
      executionDisptacher - a TaskExecutionDispatcher instance.
    • isResubmit

      public boolean isResubmit()
      Determine whether this task should be resubmitted by the server.
      Specified by:
      isResubmit in interface Task<T>
      Returns:
      true to indicate this task will be resubmitted, false otherwise.
      Since:
      4.1
    • setResubmit

      public Task<T> setResubmit(boolean resubmit)
      Specify whether this task should be resubmitted by the server.
      Specified by:
      setResubmit in interface Task<T>
      Parameters:
      resubmit - true to indicate this task will be resubmitted, false otherwise.
      Returns:
      this task, for method chaining.
      Since:
      4.1
    • getMaxResubmits

      public int getMaxResubmits()
      Description copied from interface: Task
      Get the maximum number of times a task can resubmit itself.
      Specified by:
      getMaxResubmits in interface Task<T>
      Returns:
      a positive integer, or -1 if this attribute was never set, in which case the value set in the job SLA will be used.
    • setMaxResubmits

      public Task<T> setMaxResubmits(int maxResubmits)
      Description copied from interface: Task
      Set the maximum number of times a task can resubmit itself. If the specified value is greater than or equal to zero, it will override the job-wide value provided by JobSLA.getMaxTaskResubmits().
      Specified by:
      setMaxResubmits in interface Task<T>
      Parameters:
      maxResubmits - the maximum number of resubmits.
      Returns:
      this task, for method chaining.
    • getTaskClassLoader

      public ClassLoader getTaskClassLoader()
      Get the class loader used to load this task, or the object it wraps if any.
      Specified by:
      getTaskClassLoader in interface Task<T>
      Returns:
      a ClassLoader instance.
      Since:
      5.0
    • run

      public void run()
      Specified by:
      run in interface Runnable
    • isInterruptible

      public boolean isInterruptible()
      Description copied from interface: Interruptibility
      Determine whether the thread executing the task can be interrupted.
      Specified by:
      isInterruptible in interface Interruptibility
      Returns:
      true if the task can be interrupted, false otherwise.
    • getNode

      public Node getNode()
      Description copied from interface: Task
      Get the node this task is executing in, if any.
      Specified by:
      getNode in interface Task<T>
      Returns:
      A Node instance, or null if this task is not running in a node, e.g. in a client local executor.
    • setNode

      public Task<T> setNode(Node node)
      Set the node in which this task is executing, if any.
      Specified by:
      setNode in interface Task<T>
      Parameters:
      node - the JPPF node instance.
      Returns:
      this task, for method chaining.
    • getJob

      public JPPFDistributedJob getJob()
      Description copied from interface: Task
      Get the job this task is a part of.
      Specified by:
      getJob in interface Task<T>
      Returns:
      the job as an instance of JPPFDistributedJob.
    • setJob

      public Task<T> setJob(JPPFDistributedJob job)
      set the job this task is a part of.
      Specified by:
      setJob in interface Task<T>
      Parameters:
      job - the job as an instance of JPPFDistributedJob.
      Returns:
      this task, for method call chaining.