Package org.jppf.client
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionorg.jppf.node.protocol.Task<?>Add a task to this job.org.jppf.node.protocol.Task<?>Add aRunnabletask to this job.org.jppf.node.protocol.Task<?>Add a POJO task to this job.org.jppf.node.protocol.Task<?>Add aCallabletask to this job.org.jppf.node.protocol.Task<?>add(org.jppf.node.protocol.Task<?> task) Add aTaskto this job.org.jppf.node.protocol.Task<?>add(org.jppf.utils.JPPFCallable<?> callable) Add aCallabletask to this job.org.jppf.node.protocol.Task<?>add(org.jppf.utils.JPPFRunnableTask runnable) Add aJPPFRunnableTasktask to this job.voidAdd the specified tasks to this job in a bulk operation.addDependencies(Collection<JPPFJob> dependencies) Add the specified jobs as dependencies to this job.addDependencies(JPPFJob... dependencies) Add the specified jobs as dependencies to this job.voidaddJobListener(JobListener listener) Add a listener to the list of job listeners.List<org.jppf.node.protocol.Task<?>>Wait until all execution results of the tasks in this job have been collected.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.booleancancel()Cancel this job unconditionally.booleancancel(boolean mayInterruptIfRunning) voidfireJobEvent(JobEvent.Type type, org.jppf.execute.ExecutorChannel<ClientTaskBundle> channel, List<org.jppf.node.protocol.Task<?>> tasks) Notify all listeners of the specified event type.List<org.jppf.node.protocol.Task<?>>get()List<org.jppf.node.protocol.Task<?>>List<org.jppf.node.protocol.Task<?>>Get the list of currently available task execution results.List<org.jppf.node.protocol.Task<?>>Get the list of tasks to execute.<T> JobPersistence<T>Get the persistence manager that enables saving and restoring the state of this job.booleanDetermine whether the tasks in this job form a dependencies graph.booleanbooleanisDone()Iterator<org.jppf.node.protocol.Task<?>>iterator()voidremoveJobListener(JobListener listener) Remove a listener from the list of job listeners.voidresultsReceived(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.setCascadeCancellation(boolean cascadeCancellation) Set whether cancellation of this job should trigger the cancellation of the jobs that depend on it.Set this job's dependency id.setDependencyId(Supplier<String> idSupplier) Set this job's dependency id from the specified id supplier.setDependencyId(JobDependencyIdSupplier idSupplier) Set this job's dependency id from a dependency id supplier.setGraphRoot(boolean graphRoot) Set whether this job is a root in a job dependency graph.Convenience method to set this job's name as dpeendency id.<T> JPPFJobsetPersistenceManager(JobPersistence<T> persistenceManager) Set the persistence manager that enables saving and restoring the state of this job.Convenience method to set this job's uuid as dependency id.Methods inherited from class org.jppf.client.AbstractJPPFJob
addJobStatusListener, equals, executedTaskCount, fireStatusChangeEvent, getCancelledFlag, getCancellingFlag, getClientSLA, getDataProvider, getMetadata, getName, getResults, getSLA, getStatus, getTaskCount, getUuid, hashCode, isBlocking, readResolve, removeJobStatusListener, setBlocking, setClientSLA, setDataProvider, setMetadata, setName, setSLA, setStatus, toString, unexecutedTaskCountMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
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
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
Get the list of tasks to execute.- Returns:
- a list of objects.
-
addAll
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 ofTask, annotated withJPPFRunnable, or an instance ofRunnableorCallable.- Parameters:
taskObject- the task to add to this job.args- arguments to use with a JPPF-annotated class.- Returns:
- an instance of
Taskthat is either the same as the input if the input is a subclass ofAbstractTask, or a wrapper around the input object in the other cases. - Throws:
org.jppf.JPPFException- if one of the tasks is neither aTaskor 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 perClass.getSimpleName().taskObject- the task to add to this job.args- arguments to use with a JPPF-annotated class.- Returns:
- an instance of
Taskthat is a wrapper around the input task object. - Throws:
org.jppf.JPPFException- if one of the tasks is neither aTaskor a JPPF-annotated class.
-
add
public org.jppf.node.protocol.Task<?> add(org.jppf.node.protocol.Task<?> task) throws org.jppf.JPPFException Add aTaskto this job. When the task is an instance ofTaskNode, 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
Taskthat is either the same as the input if the input is a subclass ofAbstractTask, or a wrapper around the input object in the other cases. - Throws:
org.jppf.JPPFException- if one of the tasks is neither aTaskor a JPPF-annotated class.- Since:
- 5.0
-
add
Add aRunnabletask to this job.Note: it is recommended to use
add(JPPFRunnableTask)whenever possible instead. This ensures that the providedRunnableisserializable.- Parameters:
runnable- the runnable task to add to this job.- Returns:
- an instance of
Taskthat is either the same as the input if the input is a subclass ofAbstractTask, or a wrapper around the input object in the other cases. - Throws:
org.jppf.JPPFException- if one of the tasks is neither aTaskor 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 aJPPFRunnableTasktask to this job.- Parameters:
runnable- the runnable task to add to this job.- Returns:
- an instance of
Taskthat is either the same as the input if the input is a subclass ofAbstractTask, or a wrapper around the input object in the other cases. - Throws:
org.jppf.JPPFException- if one of the tasks is neither aTaskor a JPPF-annotated class.- Since:
- 6.1
-
add
Add aCallabletask to this job.Note: it is recommended to use
add(JPPFCallable)whenever possible instead. This ensures that the providedCallableisserializable.- Parameters:
callable- the callable task to add to this job.- Returns:
- an instance of
Taskthat is either the same as the input if the input is a subclass ofAbstractTask, or a wrapper around the input object in the other cases. - Throws:
org.jppf.JPPFException- if one of the tasks is neither aTaskor 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 aCallabletask to this job.- Parameters:
callable- the callable task to add to this job.- Returns:
- an instance of
Taskthat is either the same as the input if the input is a subclass ofAbstractTask, or a wrapper around the input object in the other cases. - Throws:
org.jppf.JPPFException- if one of the tasks is neither aTaskor a JPPF-annotated class.- Since:
- 6.1
-
addJobListener
Add a listener to the list of job listeners.- Parameters:
listener- aJobListenerinstance.
-
removeJobListener
Remove a listener from the list of job listeners.- Parameters:
listener- aJobListenerinstance.
-
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
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
JobPersistenceinstance.
-
setPersistenceManager
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- aJobPersistenceinstance.- Returns:
- this job, for method chaining.
-
iterator
-
awaitResults
Wait until all execution results of the tasks in this job have been collected. This method is equivalent toget(), except that it doesn't raise an exception.- Returns:
- the list of resulting tasks.
- Since:
- 4.2
-
awaitResults
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 toget(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
nullif the timeout expired before all results were received. - Since:
- 4.2
-
getAllResults
Get the list of currently available task execution results. This method is a shortcut forgetResults().getResultsList().- Returns:
- a list of
Taskinstances, possibly empty. - Since:
- 4.2
-
cancel
public boolean cancel()Cancel this job unconditionally. This method is equivalent to callingcancel(true).- Returns:
falseif the job could not be cancelled, typically because it has already completed normally;trueotherwise.- Since:
- 4.2
-
cancel
public boolean cancel(boolean mayInterruptIfRunning) -
isCancelled
public boolean isCancelled()- Specified by:
isCancelledin interfaceFuture<List<org.jppf.node.protocol.Task<?>>>- Since:
- 4.2
-
isDone
public boolean isDone() -
get
- Specified by:
getin interfaceFuture<List<org.jppf.node.protocol.Task<?>>>- Throws:
InterruptedExceptionExecutionException- Since:
- 4.2
-
get
public List<org.jppf.node.protocol.Task<?>> get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException - Specified by:
getin interfaceFuture<List<org.jppf.node.protocol.Task<?>>>- Throws:
InterruptedExceptionExecutionExceptionTimeoutException- 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 aJobEventnotification.
-
hasTaskGraph
public boolean hasTaskGraph()Determine whether the tasks in this job form a dependencies graph.- Returns:
trueif the tasks form a graph,falseif all the tasks are independant from each other.- Since:
- 6.2
-
setDependencyId
Set this job's dependency id from the specified id supplier. This is a shortcut forgetSLA().getDependencySpec().setId(idSupplier.get()).- Parameters:
idSupplier- adependency id supplierwhich 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
Set this job's dependency id from a dependency id supplier. This is a shortcut forgetSLA().getDependencySpec().setId(idSupplier.getId(this)).- Parameters:
idSupplier- adependency id supplierwhich 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
Set this job's dependency id. This is a shortcut forgetSLA().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
Convenience method to set this job's uuid as dependency id. This is a shortcut forsetDependencyId(getUuid()).- Returns:
- this job, for method call chaining.
- Since:
- 6.2
- See Also:
-
JobDependencySpec.setId(String)
-
setNameAsDependencyId
Convenience method to set this job's name as dpeendency id. This is a shortcut forsetDependencyId(getName()).- Returns:
- this job, for method call chaining.
- Since:
- 6.2
- See Also:
-
JobDependencySpec.setId(String)
-
addDependencies
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
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
Set whether this job is a root in a job dependency graph. This is a shortcut forgetSLA().getDependencySpec().setGraphRoot(graphRoot).- Parameters:
graphRoot-trueif the job is a root in the dependency graph and should be removed from the graph after completion,falseotherwise.- Returns:
- this job, for method call chaining.
- Since:
- 6.2
- See Also:
-
JobDependencySpec.setGraphRoot(boolean)
-
setCascadeCancellation
Set whether cancellation of this job should trigger the cancellation of the jobs that depend on it. This is a shortcut forgetSLA().getDependencySpec().setCascadeCancellation(cascadeCancellation).- Parameters:
cascadeCancellation-true(the default) if cancellation of this job should trigger the cancellation of its dependents,falseotherwise.- Returns:
- this job, for method call chaining.
- Since:
- 6.2
- See Also:
-
JobDependencySpec.setCascadeCancellation(boolean)
-