org.jppf.server.protocol
Class JPPFTask

java.lang.Object
  extended by org.jppf.server.protocol.JPPFTask
All Implemented Interfaces:
Serializable, Runnable
Direct Known Subclasses:
CommandLineTask, HelloTask, JPPFAnnotatedTask, JPPFExceptionResult

public abstract class JPPFTask
extends Object
implements Runnable, Serializable

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 JPPFTask {
   public void run() {
     // do the calculation ...
     setResult(myResult);
   }
 }
 

Author:
Laurent Cohen
See Also:
Serialized Form

Field Summary
protected  List<JPPFTaskListener> listeners
          List of listeners for this task.
 
Constructor Summary
JPPFTask()
          Default constructor.
 
Method Summary
 void addJPPFTaskListener(JPPFTaskListener listener)
          Add a connection status listener to this connection's list of listeners.
 void fireNotification(Serializable source)
          Notify all listeners that an event has occurred within this task.
 DataProvider getDataProvider()
          Get the provider of shared data for this task.
 Exception getException()
          Get the exception that was raised by this task's execution.
 String getId()
          Get the user-assigned id for this task.
protected  List<JPPFTaskListener> getListeners()
          Get the list of listeners for this task.
 int getPosition()
          Returns the position of this task at the submission.
 Object getResult()
          Get the result of the task execution.
 Object getTaskObject()
          Get the JPPFRunnable-annotated object or POJO wrapped by this task.
 long getTimeout()
          Get the timeout for this task.
 String getTimeoutDate()
          Get the timeout date for this task.
 SimpleDateFormat getTimeoutDateFormat()
          Deprecated. use getTimeoutFormat() instead.
 String getTimeoutFormat()
          Get the format of timeout date for this task.
 JPPFSchedule getTimeoutSchedule()
          Get the task timeout schedule configuration.
 void onCancel()
          Callback invoked when this task is cancelled.
 void onRestart()
          Callback invoked when this task is restarted.
 void onTimeout()
          Callback invoked when this task times out.
 void removeJPPFTaskListener(JPPFTaskListener listener)
          Remove a connection status listener from this connection's list of listeners.
 void setDataProvider(DataProvider dataProvider)
          Set the provider of shared data for this task.
 void setException(Exception exception)
          Sets the exception that was raised by this task's execution in the run method.
 void setId(String id)
          Set the user-assigned id for this task.
 void setPosition(int position)
          Sets the position of this task into the submission.
 void setResult(Object result)
          Set the result of the task execution.
 void setTimeout(long timeout)
          Set the timeout for this task.
 void setTimeoutDate(String timeoutDate, SimpleDateFormat timeoutDateFormat)
          Deprecated. use getTimeoutFormat(String, String) instead.
 void setTimeoutDate(String timeoutDate, String format)
          Set the timeout date for this task.
 void setTimeoutSchedule(JPPFSchedule timeoutSchedule)
          Get the task timeout schedule configuration.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.lang.Runnable
run
 

Field Detail

listeners

protected transient List<JPPFTaskListener> listeners
List of listeners for this task.

Constructor Detail

JPPFTask

public JPPFTask()
Default constructor.

Method Detail

getResult

public Object getResult()
Get the result of the task execution.

Returns:
the result as an array of bytes.

setResult

public void setResult(Object result)
Set the result of the task execution.

Parameters:
result - the result of this task's execution.

getException

public Exception getException()
Get the exception that was raised by this task's execution. If the task raised a Throwable, the exception is embedded into a JPPFException.

Returns:
a Exception instance, or null if no exception was raised.

setException

public void setException(Exception exception)
Sets the exception that was raised by this task's execution in the run method. The exception is set by the JPPF framework.

Parameters:
exception - a ClientApplicationException instance.

getDataProvider

public DataProvider getDataProvider()
Get the provider of shared data for this task.

Returns:
a DataProvider instance.

setDataProvider

public void setDataProvider(DataProvider dataProvider)
Set the provider of shared data for this task.

Parameters:
dataProvider - a DataProvider instance.

getPosition

public final int getPosition()
Returns the position of this task at the submission.

Returns:
Returns the position of this task at the submission.

setPosition

public final void setPosition(int position)
Sets the position of this task into the submission.

Parameters:
position - The position of this task into the submission.

addJPPFTaskListener

public void addJPPFTaskListener(JPPFTaskListener listener)
Add a connection status listener to this connection's list of listeners.

Parameters:
listener - the listener to add to the list.

removeJPPFTaskListener

public void removeJPPFTaskListener(JPPFTaskListener listener)
Remove a connection status listener from this connection's list of listeners.

Parameters:
listener - the listener to remove from the list.

fireNotification

public void fireNotification(Serializable source)
Notify all listeners that an event has occurred within this task.

Parameters:
source - an object describing the event, must be serializable.

getListeners

protected List<JPPFTaskListener> getListeners()
Get the list of listeners for this task.

Returns:
a list of JPPFTaskListener instances.

getTimeout

public long getTimeout()
Get the timeout for this task.

Returns:
the timeout in milliseconds.

setTimeout

public void setTimeout(long timeout)
Set the timeout for this task.

Parameters:
timeout - the timeout in milliseconds.

getTimeoutDate

public String getTimeoutDate()
Get the timeout date for this task.

Returns:
the date in string format.

getTimeoutDateFormat

public SimpleDateFormat getTimeoutDateFormat()
Deprecated. use getTimeoutFormat() instead.

Get the format of timeout date for this task.

Returns:
a SimpleDateFormat instance.

getTimeoutFormat

public String getTimeoutFormat()
Get the format of timeout date for this task.

Returns:
the timeout date format as a string pattern, as decribed in the specification for SimpleDateFormat.

setTimeoutDate

public void setTimeoutDate(String timeoutDate,
                           SimpleDateFormat timeoutDateFormat)
Deprecated. use getTimeoutFormat(String, String) instead.

Set the timeout date for this task.
Calling this method will reset the timeout value, as both timeout duration and timeout date are mutually exclusive.

Parameters:
timeoutDate - the date to set in string representation.
timeoutDateFormat - the format of of the date to set.

setTimeoutDate

public void setTimeoutDate(String timeoutDate,
                           String format)
Set the timeout date for this task.
Calling this method will reset the timeout value, as both timeout duration and timeout date are mutually exclusive.

Parameters:
timeoutDate - the date to set in string representation.
format - the format of of the date to set, as described in the specfication for SimpleDateFormat.
See Also:
SimpleDateFormat

getId

public String getId()
Get the user-assigned id for this task.

Returns:
the id as a string.

setId

public void setId(String id)
Set the user-assigned id for this task.

Parameters:
id - the id as a string.

onCancel

public void onCancel()
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.


onRestart

public void onRestart()
Callback invoked when this task is restarted. The default implementation does nothing and should be overriden by subclasses that desire to implement a specific behaviour on restart.


onTimeout

public void onTimeout()
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.


getTaskObject

public Object getTaskObject()
Get the JPPFRunnable-annotated object or POJO wrapped by this task.

Returns:
an objet or class that is JPPF-annotated.

getTimeoutSchedule

public JPPFSchedule getTimeoutSchedule()
Get the task timeout schedule configuration.

Returns:
a JPPFScheduleConfiguration instance.

setTimeoutSchedule

public void setTimeoutSchedule(JPPFSchedule timeoutSchedule)
Get the task timeout schedule configuration.

Parameters:
timeoutSchedule - a JPPFScheduleConfiguration instance.


Copyright © 2005-2010 JPPF Team.