Package org.jppf.node.protocol
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
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<V> Vcompute(JPPFCallable<V> callable) Compute a value on the client-side, as the result of the execution of aJPPFCallable.fireNotification(Object userObject, boolean sendViaJmx) Causes the task to send a notification to all listeners.Get the provider of shared data for this task.getId()Get the user-assigned id for this task.getJob()Get the job this task is a part of.intGet the maximum number of times a task can resubmit itself.getNode()Get the node this task is executing in, if any.intReturns the position of this element in its container.Get the result of the task execution.Get the class loader used to load this task, or the object it wraps if any.Get theJPPFRunnable-annotated object or POJO wrapped by this task.Get the Throwable that was raised by this task's execution.Get the task timeout schedule configuration.booleanisInNode()Determine whether this task is executing within a node, or locally on the client side.booleanDetermine whether the thread executing the task can be interrupted.booleanDetermine whether this task should be resubmitted by the server.voidonCancel()Callback invoked when this task is cancelled.voidCallback invoked when this task times out.voidrun()setDataProvider(DataProvider dataProvider) Set the provider of shared data for this task.voidsetExecutionDispatcher(TaskExecutionDispatcher executionDisptacher) Set the task notification dispatcher onto this task.Set the user-assigned id for this task.setInNode(boolean inNode) Determine whether this task is executing within a node, or locally on the client side.setJob(JPPFDistributedJob job) set the job this task is a part of.setMaxResubmits(int maxResubmits) Set the maximum number of times a task can resubmit itself.Set the node in which this task is executing, if any.setPosition(int position) Set the position of this element in its container.setResubmit(boolean resubmit) Specify whether this task should be resubmitted by the server.Set the result of the task execution.setThrowable(Throwable throwable) Sets the Throwable that was raised by this task's execution in therun()method.setTimeoutSchedule(JPPFSchedule timeoutSchedule) Get the task timeout schedule configuration.
-
Constructor Details
-
AbstractTask
public AbstractTask()
-
-
Method Details
-
getResult
Description copied from interface:TaskGet the result of the task execution. -
setResult
Description copied from interface:TaskSet the result of the task execution. -
getThrowable
Description copied from interface:TaskGet the Throwable that was raised by this task's execution.- Specified by:
getThrowablein interfaceTask<T>- Returns:
- a
Throwableinstance, ornullif no exception was raised.
-
setThrowable
Description copied from interface:TaskSets the Throwable that was raised by this task's execution in therun()method. The Throwable is normally set by the JPPF framework.- Specified by:
setThrowablein interfaceTask<T>- Parameters:
throwable- aThrowableinstance.- Returns:
- this task, for method chaining.
-
getDataProvider
Description copied from interface:TaskGet the provider of shared data for this task.- Specified by:
getDataProviderin interfaceTask<T>- Returns:
- a
DataProviderinstance.
-
setDataProvider
Set the provider of shared data for this task.- Specified by:
setDataProviderin interfaceTask<T>- Parameters:
dataProvider- aDataProviderinstance.- Returns:
- this task, for method chaining.
-
getId
Description copied from interface:TaskGet the user-assigned id for this task. -
setId
Description copied from interface:TaskSet the user-assigned id for this task. -
onCancel
public void onCancel()Description copied from interface:TaskCallback 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. -
onTimeout
public void onTimeout()Description copied from interface:TaskCallback 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. -
getTaskObject
Description copied from interface:TaskGet theJPPFRunnable-annotated object or POJO wrapped by this task.- Specified by:
getTaskObjectin interfaceTask<T>- Returns:
- an object or class that is JPPF-annotated.
-
getTimeoutSchedule
Description copied from interface:TaskGet the task timeout schedule configuration.- Specified by:
getTimeoutSchedulein interfaceTask<T>- Returns:
- a
JPPFScheduleinstance.
-
setTimeoutSchedule
Description copied from interface:TaskGet the task timeout schedule configuration.- Specified by:
setTimeoutSchedulein interfaceTask<T>- Parameters:
timeoutSchedule- aJPPFScheduleinstance.- Returns:
- this task, for method chaining.
-
getPosition
public int getPosition()Returns the position of this element in its container.- Specified by:
getPositionin interfacePositionalElement<T>- Returns:
- the position of this element as an
int.
-
setPosition
Set the position of this element in its container.- Specified by:
setPositionin interfacePositionalElement<T>- Parameters:
position- the position of this task as anint.- Returns:
- this element, for method call chaining.
-
isInNode
public boolean isInNode()Description copied from interface:TaskDetermine whether this task is executing within a node, or locally on the client side. -
setInNode
Determine whether this task is executing within a node, or locally on the client side. -
compute
Description copied from interface:TaskCompute a value on the client-side, as the result of the execution of aJPPFCallable.Any
Throwableraised in the callable'scall()method will be thrown as the result of this method. If the Throwable is an instance ofExceptionor one of its subclasses, it is thrown as such, otherwise it is wrapped into aJPPFException.- Specified by:
computein interfaceTask<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 aThrowablebeing raised.
-
fireNotification
Description copied from interface:TaskCauses 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 theRunnable.run(),Task.onTimeout()orTask.onCancel()methods.If the parameter
sendViaJmxistrue, then a notfication will also be sent by theJPPFNodeTaskMonitorMBeanmbean, otherwise only local listeners will be notified.- Specified by:
fireNotificationin interfaceTask<T>- Parameters:
userObject- a user-defined object to send as part of the notification.sendViaJmx- iftruethen 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
Set the task notification dispatcher onto this task.- Parameters:
executionDisptacher- aTaskExecutionDispatcherinstance.
-
isResubmit
public boolean isResubmit()Determine whether this task should be resubmitted by the server.- Specified by:
isResubmitin interfaceTask<T>- Returns:
trueto indicate this task will be resubmitted,falseotherwise.- Since:
- 4.1
-
setResubmit
Specify whether this task should be resubmitted by the server.- Specified by:
setResubmitin interfaceTask<T>- Parameters:
resubmit-trueto indicate this task will be resubmitted,falseotherwise.- Returns:
- this task, for method chaining.
- Since:
- 4.1
-
getMaxResubmits
public int getMaxResubmits()Description copied from interface:TaskGet the maximum number of times a task can resubmit itself.- Specified by:
getMaxResubmitsin interfaceTask<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
Description copied from interface:TaskSet 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 byJobSLA.getMaxTaskResubmits().- Specified by:
setMaxResubmitsin interfaceTask<T>- Parameters:
maxResubmits- the maximum number of resubmits.- Returns:
- this task, for method chaining.
-
getTaskClassLoader
Get the class loader used to load this task, or the object it wraps if any.- Specified by:
getTaskClassLoaderin interfaceTask<T>- Returns:
- a
ClassLoaderinstance. - Since:
- 5.0
-
run
public void run() -
isInterruptible
public boolean isInterruptible()Description copied from interface:InterruptibilityDetermine whether the thread executing the task can be interrupted.- Specified by:
isInterruptiblein interfaceInterruptibility- Returns:
trueif the task can be interrupted,falseotherwise.
-
getNode
Description copied from interface:TaskGet the node this task is executing in, if any. -
setNode
Set the node in which this task is executing, if any. -
getJob
Description copied from interface:TaskGet the job this task is a part of.- Specified by:
getJobin interfaceTask<T>- Returns:
- the job as an instance of
JPPFDistributedJob.
-
setJob
set the job this task is a part of.- Specified by:
setJobin interfaceTask<T>- Parameters:
job- the job as an instance ofJPPFDistributedJob.- Returns:
- this task, for method call chaining.
-