ome.util.tasks
Class Task

java.lang.Object
  extended by ome.util.tasks.Task
All Implemented Interfaces:
Runnable
Direct Known Subclasses:
SimpleTask

public abstract class Task
extends Object
implements Runnable

Base task class which defines the template methods for running a task. The base class ensures that a ServiceFactory and a Properties instance will be available in the task runtime. Task implements Runnable and therefore can be placed directly in a Thread for execution. The Runnable.run() method, however, has been marked final and instead other Task methods must be overwritten. Execution begins with a call to init(), then doTask() is called in a try/catch/finally block. If doTask() returns normally, the value of completedSuccessfully() will be true, otherwise it will remain false and execution will be passed to handleException(RuntimeException). Finally, close() is called. Subclasses should most likely check the value of completedSuccessfully() before attempting close();

Since:
3.0-Beta1
Version:
$Revision: 1568 $, $Date: 2007-05-30 20:03:03 +0100 (Wed, 30 May 2007) $
Author:
Josh Moore, josh.moore at gmx.de
See Also:
Runnable

Constructor Summary
protected Task(Properties properties)
          Protected constructor for subclasses who may not wish to use sf.
  Task(ServiceFactory serviceFactory, Properties properties)
          Sole constructor.
 
Method Summary
abstract  void close()
          Performs cleanup of the Task.
 boolean completedSuccessfully()
          returns true if doTask() returned normally or false if an exception was thrown.
abstract  void doTask()
          Performs the actual work of the Task.
protected  String enumValue(Enum e)
          returns the value of the string representation of the enum argument from the Properties instance for this task.
 org.apache.commons.logging.Log getLogger()
          returns the logger instance active for this class.
 Properties getProperties()
          returns the Properties instance defined during construction.
 ServiceFactory getServiceFactory()
          returns the ServiceFactory instance provided during construction.
abstract  void handleException(RuntimeException re)
          If an exception is thrown during doTask(), then it will be passed to the handleException(RuntimeException) method during the catch-block of run().
abstract  void init()
          Performs initialization of the Task.
 void run()
          Marked final so that the Task lifecycle will never vary.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Task

protected Task(Properties properties)
Protected constructor for subclasses who may not wish to use sf.


Task

public Task(ServiceFactory serviceFactory,
            Properties properties)
Sole constructor. Requires a non-null ServiceFactory. If a null Properties instance is provided, the System properties will be used.

Parameters:
serviceFactory - Cannot be null.
properties - Context variables for the task. Optional (can be null).
Method Detail

getServiceFactory

public final ServiceFactory getServiceFactory()
returns the ServiceFactory instance provided during construction.

Returns:
ServiceFactory. Never null.

getProperties

public final Properties getProperties()
returns the Properties instance defined during construction. If none was provided at construction, the System properties will be used.

Returns:
Properties. Never null.

getLogger

public final org.apache.commons.logging.Log getLogger()
returns the logger instance active for this class. This is not a static instance.

Returns:
logger. Not null.

completedSuccessfully

public final boolean completedSuccessfully()
returns true if doTask() returned normally or false if an exception was thrown.


run

public final void run()
Marked final so that the Task lifecycle will never vary.

Specified by:
run in interface Runnable

init

public abstract void init()
Performs initialization of the Task. Subclasses should make a best effort attempt to not throw any exceptions.


doTask

public abstract void doTask()
                     throws RuntimeException
Performs the actual work of the Task. Execution takes place in a try/catch/finally-block, and subclasses can throw any RuntimeException.

Throws:
RuntimeException

handleException

public abstract void handleException(RuntimeException re)
If an exception is thrown during doTask(), then it will be passed to the handleException(RuntimeException) method during the catch-block of run(). Subclasses can handle the exception or rethrow.

Parameters:
re - Non-null.

close

public abstract void close()
Performs cleanup of the Task. Subclasses should make a best effort attempt to not throw any exceptions.


enumValue

protected String enumValue(Enum e)
returns the value of the string representation of the enum argument from the Properties instance for this task.

Parameters:
e - Any enumeration. May not be null.


OmeroJava Api

Version: Beta-4.1.1-r5927-b91

Copyright © 2009 The University of Dundee. All Rights Reserved.