Class JPPFJob

All Implemented Interfaces:
Serializable, Iterable<org.jppf.node.protocol.Task<?>>, Future<List<org.jppf.node.protocol.Task<?>>>, JobStatusHandler, org.jppf.node.protocol.JPPFDistributedJob

public class JPPFJob extends AbstractJPPFJob<JPPFJob> implements Iterable<org.jppf.node.protocol.Task<?>>, Future<List<org.jppf.node.protocol.Task<?>>>
Instances of this class represent a JPPF job and hold all the required elements: tasks, execution policy, task listener, data provider, priority, blocking indicator.

This class also provides the API for handling JPPF-annotated tasks and POJO tasks.

All jobs have a name. It can be specified by calling setName(String name). If left unspecified, JPPF will automatically assign a uuid as its value.

Author:
Laurent Cohen
See Also:
  • Constructor Details

    • JPPFJob

      public JPPFJob()
      Default constructor, creates a blocking job with no data provider, default SLA values and a priority of 0. This constructor generates a pseudo-random id as a string of 32 hexadecimal characters.
    • JPPFJob

      public JPPFJob(String jobUuid)
      Default constructor, creates a blocking job with no data provider, default SLA values and a priority of 0. This constructor generates a pseudo-random id as a string of 32 hexadecimal characters.
      Parameters:
      jobUuid - the uuid to assign to this job.
  • Method Details

    • getJobTasks

      public List<org.jppf.node.protocol.Task<?>> getJobTasks()
      Get the list of tasks to execute.
      Returns:
      a list of objects.
    • addAll

      public void addAll(List<org.jppf.node.protocol.Task<?>> tasks) throws org.jppf.JPPFException
      Add the specified tasks to this job in a bulk operation.
      Parameters:
      tasks - the list of tasks to add.
      Throws:
      org.jppf.JPPFException - if any error occurs.
      Since:
      6.0
    • add

      public org.jppf.node.protocol.Task<?> add(Object taskObject, Object... args) throws org.jppf.JPPFException
      Add a task to this job. This method is for adding a task that is either an instance of Task, annotated with JPPFRunnable, or an instance of Runnable or Callable.
      Parameters:
      taskObject - the task to add to this job.
      args - arguments to use with a JPPF-annotated class.
      Returns:
      an instance of Task that is either the same as the input if the input is a subclass of AbstractTask, or a wrapper around the input object in the other cases.
      Throws:
      org.jppf.JPPFException - if one of the tasks is neither a Task or a JPPF-annotated class.
    • add

      public org.jppf.node.protocol.Task<?> add(String method, Object taskObject, Object... args) throws org.jppf.JPPFException
      Add a POJO task to this job. The POJO task is identified as a method name associated with either an object for a non-static method, or a class for a static method or for a constructor.
      Parameters:
      method - the name of the method to execute. For a constructor, this should be identical to the simple name of the class as per Class.getSimpleName().
      taskObject - the task to add to this job.
      args - arguments to use with a JPPF-annotated class.
      Returns:
      an instance of Task that is a wrapper around the input task object.
      Throws:
      org.jppf.JPPFException - if one of the tasks is neither a Task or a JPPF-annotated class.
    • add

      public org.jppf.node.protocol.Task<?> add(org.jppf.node.protocol.Task<?> task) throws org.jppf.JPPFException
      Add a Task to this job. When the task is an instance of TaskNode, the entire dependency graph rooted at this task is added as well.
      Parameters:
      task - the task to add to this job.
      Returns:
      an instance of Task that is either the same as the input if the input is a subclass of AbstractTask, or a wrapper around the input object in the other cases.
      Throws:
      org.jppf.JPPFException - if one of the tasks is neither a Task or a JPPF-annotated class.
      Since:
      5.0
    • add

      public org.jppf.node.protocol.Task<?> add(Runnable runnable) throws org.jppf.JPPFException
      Add a Runnable task to this job.

      Note: it is recommended to use add(JPPFRunnableTask) whenever possible instead. This ensures that the provided Runnable is serializable.

      Parameters:
      runnable - the runnable task to add to this job.
      Returns:
      an instance of Task that is either the same as the input if the input is a subclass of AbstractTask, or a wrapper around the input object in the other cases.
      Throws:
      org.jppf.JPPFException - if one of the tasks is neither a Task or a JPPF-annotated class.
      Since:
      5.0
    • add

      public org.jppf.node.protocol.Task<?> add(org.jppf.utils.JPPFRunnableTask runnable) throws org.jppf.JPPFException
      Add a JPPFRunnableTask task to this job.
      Parameters:
      runnable - the runnable task to add to this job.
      Returns:
      an instance of Task that is either the same as the input if the input is a subclass of AbstractTask, or a wrapper around the input object in the other cases.
      Throws:
      org.jppf.JPPFException - if one of the tasks is neither a Task or a JPPF-annotated class.
      Since:
      6.1
    • add

      public org.jppf.node.protocol.Task<?> add(Callable<?> callable) throws org.jppf.JPPFException
      Add a Callable task to this job.

      Note: it is recommended to use add(JPPFCallable) whenever possible instead. This ensures that the provided Callable is serializable.

      Parameters:
      callable - the callable task to add to this job.
      Returns:
      an instance of Task that is either the same as the input if the input is a subclass of AbstractTask, or a wrapper around the input object in the other cases.
      Throws:
      org.jppf.JPPFException - if one of the tasks is neither a Task or a JPPF-annotated class.
      Since:
      5.0
    • add

      public org.jppf.node.protocol.Task<?> add(org.jppf.utils.JPPFCallable<?> callable) throws org.jppf.JPPFException
      Add a Callable task to this job.
      Parameters:
      callable - the callable task to add to this job.
      Returns:
      an instance of Task that is either the same as the input if the input is a subclass of AbstractTask, or a wrapper around the input object in the other cases.
      Throws:
      org.jppf.JPPFException - if one of the tasks is neither a Task or a JPPF-annotated class.
      Since:
      6.1
    • addJobListener

      public void addJobListener(JobListener listener)
      Add a listener to the list of job listeners.
      Parameters:
      listener - a JobListener instance.
    • removeJobListener

      public void removeJobListener(JobListener listener)
      Remove a listener from the list of job listeners.
      Parameters:
      listener - a JobListener instance.
    • fireJobEvent

      public void fireJobEvent(JobEvent.Type type, org.jppf.execute.ExecutorChannel<ClientTaskBundle> channel, List<org.jppf.node.protocol.Task<?>> tasks)
      Notify all listeners of the specified event type.
      Parameters:
      type - the type of the event.
      channel - the channel to which a job is dispatched or from which it is returned.
      tasks - the tasks that were dispatched or returned.
    • getPersistenceManager

      public <T> JobPersistence<T> getPersistenceManager()
      Get the persistence manager that enables saving and restoring the state of this job.
      Type Parameters:
      T - the type of the keys used by the persistence manager.
      Returns:
      a JobPersistence instance.
    • setPersistenceManager

      public <T> JPPFJob setPersistenceManager(JobPersistence<T> persistenceManager)
      Set the persistence manager that enables saving and restoring the state of this job.
      Type Parameters:
      T - the type of the keys used by the persistence manager.
      Parameters:
      persistenceManager - a JobPersistence instance.
      Returns:
      this job, for method chaining.
    • iterator

      public Iterator<org.jppf.node.protocol.Task<?>> iterator()
      Specified by:
      iterator in interface Iterable<org.jppf.node.protocol.Task<?>>
    • awaitResults

      public List<org.jppf.node.protocol.Task<?>> awaitResults()
      Wait until all execution results of the tasks in this job have been collected. This method is equivalent to get(), except that it doesn't raise an exception.
      Returns:
      the list of resulting tasks.
      Since:
      4.2
    • awaitResults

      public List<org.jppf.node.protocol.Task<?>> awaitResults(long timeout)
      Wait until all execution results of the tasks in this job have been collected, or the timeout expires, whichever happens first. This method is equivalent to get(timeout, TimeUnit.MILLISECONDS), except that it doesn't raise an exception.
      Parameters:
      timeout - the maximum time to wait in milliseconds, zero or less meaning an infinite wait.
      Returns:
      the list of resulting tasks, or null if the timeout expired before all results were received.
      Since:
      4.2
    • getAllResults

      public List<org.jppf.node.protocol.Task<?>> getAllResults()
      Get the list of currently available task execution results. This method is a shortcut for getResults().getResultsList().
      Returns:
      a list of Task instances, possibly empty.
      Since:
      4.2
    • cancel

      public boolean cancel()
      Cancel this job unconditionally. This method is equivalent to calling cancel(true).
      Returns:
      false if the job could not be cancelled, typically because it has already completed normally; true otherwise.
      Since:
      4.2
    • cancel

      public boolean cancel(boolean mayInterruptIfRunning)
      Specified by:
      cancel in interface Future<List<org.jppf.node.protocol.Task<?>>>
      Since:
      4.2
    • isCancelled

      public boolean isCancelled()
      Specified by:
      isCancelled in interface Future<List<org.jppf.node.protocol.Task<?>>>
      Since:
      4.2
    • isDone

      public boolean isDone()
      Specified by:
      isDone in interface Future<List<org.jppf.node.protocol.Task<?>>>
      Since:
      4.2
    • get

      public List<org.jppf.node.protocol.Task<?>> get() throws InterruptedException, ExecutionException
      Specified by:
      get in interface Future<List<org.jppf.node.protocol.Task<?>>>
      Throws:
      InterruptedException
      ExecutionException
      Since:
      4.2
    • get

      public List<org.jppf.node.protocol.Task<?>> get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
      Specified by:
      get in interface Future<List<org.jppf.node.protocol.Task<?>>>
      Throws:
      InterruptedException
      ExecutionException
      TimeoutException
      Since:
      4.2
    • resultsReceived

      public void resultsReceived(List<org.jppf.node.protocol.Task<?>> tasks, Throwable throwable, boolean sendJobEvent)
      Called to notify that the results of a number of tasks have been received from the server.
      Parameters:
      tasks - the list of tasks whose results have been received from the server.
      throwable - the throwable that was raised while receiving the results.
      sendJobEvent - whether to emit a JobEvent notification.
    • hasTaskGraph

      public boolean hasTaskGraph()
      Determine whether the tasks in this job form a dependencies graph.
      Returns:
      true if the tasks form a graph, false if all the tasks are independant from each other.
      Since:
      6.2
    • setDependencyId

      public JPPFJob setDependencyId(Supplier<String> idSupplier)
      Set this job's dependency id from the specified id supplier. This is a shortcut for getSLA().getDependencySpec().setId(idSupplier.get()).
      Parameters:
      idSupplier - a dependency id supplier which computes a dependency id from this job's state.
      Returns:
      this job, for method call chaining.
      Since:
      6.2
      See Also:
      • JobDependencySpec.setId(String)
    • setDependencyId

      public JPPFJob setDependencyId(JobDependencyIdSupplier idSupplier)
      Set this job's dependency id from a dependency id supplier. This is a shortcut for getSLA().getDependencySpec().setId(idSupplier.getId(this)).
      Parameters:
      idSupplier - a dependency id supplier which computes a dependency id from this job's state.
      Returns:
      this job, for method call chaining.
      Since:
      6.2
      See Also:
      • JobDependencySpec.setId(String)
    • setDependencyId

      public JPPFJob setDependencyId(String id)
      Set this job's dependency id. This is a shortcut for getSLA().getDependencySpec().setId(id).
      Parameters:
      id - the id of this job in the job dependency graph.
      Returns:
      this job, for method call chaining.
      Since:
      6.2
      See Also:
      • JobDependencySpec.setId(String)
    • setUuidAsDependencyId

      public JPPFJob setUuidAsDependencyId()
      Convenience method to set this job's uuid as dependency id. This is a shortcut for setDependencyId(getUuid()).
      Returns:
      this job, for method call chaining.
      Since:
      6.2
      See Also:
      • JobDependencySpec.setId(String)
    • setNameAsDependencyId

      public JPPFJob setNameAsDependencyId()
      Convenience method to set this job's name as dpeendency id. This is a shortcut for setDependencyId(getName()).
      Returns:
      this job, for method call chaining.
      Since:
      6.2
      See Also:
      • JobDependencySpec.setId(String)
    • addDependencies

      public JPPFJob addDependencies(JPPFJob... dependencies)
      Add the specified jobs as dependencies to this job.
      Parameters:
      dependencies - the job to add as dependencies to this job.
      Returns:
      this job, for method call chaining.
      Since:
      6.2
      See Also:
      • JobDependencySpec.addDependencies(String[])
    • addDependencies

      public JPPFJob addDependencies(Collection<JPPFJob> dependencies)
      Add the specified jobs as dependencies to this job.
      Parameters:
      dependencies - the job to add as dependencies to this job.
      Returns:
      this job, for method call chaining.
      Since:
      6.2
      See Also:
      • JobDependencySpec.addDependencies(Collection)
    • setGraphRoot

      public JPPFJob setGraphRoot(boolean graphRoot)
      Set whether this job is a root in a job dependency graph. This is a shortcut for getSLA().getDependencySpec().setGraphRoot(graphRoot).
      Parameters:
      graphRoot - true if the job is a root in the dependency graph and should be removed from the graph after completion, false otherwise.
      Returns:
      this job, for method call chaining.
      Since:
      6.2
      See Also:
      • JobDependencySpec.setGraphRoot(boolean)
    • setCascadeCancellation

      public JPPFJob setCascadeCancellation(boolean cascadeCancellation)
      Set whether cancellation of this job should trigger the cancellation of the jobs that depend on it. This is a shortcut for getSLA().getDependencySpec().setCascadeCancellation(cascadeCancellation).
      Parameters:
      cascadeCancellation - true (the default) if cancellation of this job should trigger the cancellation of its dependents, false otherwise.
      Returns:
      this job, for method call chaining.
      Since:
      6.2
      See Also:
      • JobDependencySpec.setCascadeCancellation(boolean)