it.sauronsoftware.cron4j
Class Task

java.lang.Object
  extended by it.sauronsoftware.cron4j.Task
Direct Known Subclasses:
ProcessTask

public abstract class Task
extends java.lang.Object

Abstract base representation of a cron4j task.

Developers can extends this abstract class to build their own tasks.

Extending Task means, above all, implementing the execute(TaskExecutionContext) method. Within this method the task must perform its operation. If the execute() method returns regularly then the execution is considered to be successfully completed. If execute() dies throwing a RuntimeException then the task execution is considered to be failed. The supplied parameter, which is a TaskExecutionContext instance, helps the developer in integrating his task with the scheduler executor. Through the context the developer can check if the execution has been paused or stopped, and he can also push back some status informations by calling TaskExecutionContext.setCompleteness(double) and TaskExecutionContext.setStatusMessage(String).

If the custom task supports pausing, stopping and/or tracking, that should be notified by overriding canBePaused(), canBeStopped(), supportsCompletenessTracking() and/or supportsStatusTracking().

Since:
2.0
Author:
Carlo Pelliccia

Constructor Summary
Task()
          Empty constructor, does nothing.
 
Method Summary
 boolean canBePaused()
           Checks whether this task supports pause requests.
 boolean canBeStopped()
           Checks whether this task supports stop requests.
abstract  void execute(TaskExecutionContext context)
           This method is called to require a task execution, and should contains the core routine of any scheduled task.
 boolean supportsCompletenessTracking()
           Tests whether this task supports completeness tracking.
 boolean supportsStatusTracking()
           Tests whether this task supports status tracking.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Task

public Task()
Empty constructor, does nothing.

Method Detail

canBePaused

public boolean canBePaused()

Checks whether this task supports pause requests.

Default implementation returns false.

Task developers can override this method to let it return a true value, and at the same time they have to implement the execute(TaskExecutionContext) method, so that pause requests are really handled. This can be done calling regularly the TaskExecutionContext.pauseIfRequested() method during the task execution.

Returns:
true if this task can be paused; false otherwise.

canBeStopped

public boolean canBeStopped()

Checks whether this task supports stop requests.

Default implementation returns false.

Task developers can override this method to let it return a true value, and at the same time they have to implement the execute(TaskExecutionContext) method, so that stop requests are really handled. This can be done checking regularly the TaskExecutionContext.isStopped() method during the task execution.

Returns:
true if this task can be stopped; false otherwise.

supportsStatusTracking

public boolean supportsStatusTracking()

Tests whether this task supports status tracking.

Default implementation returns false.

The task developer can override this method and returns true, having care to regularly calling the TaskExecutionContext.setStatusMessage(String) method during the task execution.

Returns:
true if this task, during its execution, provides status message regularly.

supportsCompletenessTracking

public boolean supportsCompletenessTracking()

Tests whether this task supports completeness tracking.

Default implementation returns false.

The task developer can override this method and returns true, having care to regularly calling the TaskExecutionContext.setCompleteness(double) method during the task execution.

Returns:
true if this task, during its execution, provides a completeness value regularly.

execute

public abstract void execute(TaskExecutionContext context)
                      throws java.lang.RuntimeException

This method is called to require a task execution, and should contains the core routine of any scheduled task.

If the execute() method ends regularly the scheduler will consider the execution successfully completed, and this will be communicated to any SchedulerListener interested in it. If the execute() method dies throwing a RuntimeException the scheduler will consider it as a failure notification. Any SchedulerListener will be notified about the occurred exception.

Parameters:
context - The execution context.
Throws:
java.lang.RuntimeException - Task execution has somehow failed.