nl.coderight.jazz
Class Task<V>

java.lang.Object
  extended by nl.coderight.jazz.Task<V>

public abstract class Task<V>
extends Object

Use Task for (long running) background processes.

 Example:
 
 public LoadContactsTask> extends Task {
   
   public List<Contact> process() throws Exception {
      AddressBookService service = AddressBookService.getInstance();
      return service.loadContacts();
   }
   
   protected void onFinish() {
      ContactEvent contactEvent = new ContactEvent(ContactEventType.LOAD_SUCCESS);
      contactEvent.setContacts(getResult());
      postEvent(contactEvent);
   }
 
   protected void onError() {
      MessageEvent error = new MessageEvent(MessageEventType.ERROR);
      error.setMessage(getError().getMessage());
      postEvent(error);            
   }
 }
 

See Also:
Controller.executeTask(Task)

Nested Class Summary
 class Task.FixedDelaySchedule
           
 class Task.FixedRateSchedule
           
static class Task.TaskState
          Enumeration of the possible Task states.
 
Constructor Summary
Task()
           
 
Method Summary
 void cancel()
          Cancels running task.
 ProgressMonitor createProgressMonitor(String message, InputStream inputStream)
          Creates a ProgressMonitor.
 ProgressMonitor createProgressMonitor(String message, String note, int min, int max)
          Creates a standard ProgressMonitor.
 Exception getError()
          Returns the error (if set).
 V getResult()
          Returns the result from process.
 nl.coderight.jazz.Task.Schedule getSchedule()
          Returns task schedule.
 Task.TaskState getState()
          Returns current state.
 boolean hasError()
          Returns true if an error is set.
 boolean isCanceled()
          Returns true if task was canceled.
protected  void onCanceled()
          Invoked when task is canceled.
protected  void onError()
          Invoked when task has an error.
protected  void onFinish()
          Invoked when task is finished.
protected  void onStart()
          Invoked when task starts running.
 void postEvent(Event event)
          Use to post events like ProgressEvents and message events.
abstract  V process()
          Performs a (long running) operation.
 void setSchedule(nl.coderight.jazz.Task.Schedule schedule)
          Sets task schedule.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Task

public Task()
Method Detail

getState

public Task.TaskState getState()
Returns current state.

See Also:
Task.TaskState

getSchedule

public nl.coderight.jazz.Task.Schedule getSchedule()
Returns task schedule.

Since:
1.3

setSchedule

public void setSchedule(nl.coderight.jazz.Task.Schedule schedule)
Sets task schedule.

Since:
1.3
See Also:
Task.FixedRateSchedule

getError

public Exception getError()
Returns the error (if set).

Returns:
Exception

hasError

public boolean hasError()
Returns true if an error is set.

Since:
1.2

getResult

public V getResult()
Returns the result from process.

Returns:
value (can be null)

cancel

public final void cancel()
Cancels running task.


isCanceled

public boolean isCanceled()
Returns true if task was canceled.


postEvent

public final void postEvent(Event event)
Use to post events like ProgressEvents and message events.


createProgressMonitor

public final ProgressMonitor createProgressMonitor(String message,
                                                   String note,
                                                   int min,
                                                   int max)
Creates a standard ProgressMonitor.

Since:
1.1
See Also:
ProgressMonitor

createProgressMonitor

public final ProgressMonitor createProgressMonitor(String message,
                                                   InputStream inputStream)
Creates a ProgressMonitor.

Since:
1.3
See Also:
ProgressMonitor

process

public abstract V process()
                   throws Exception
Performs a (long running) operation.

Returns:
result or null if none
Throws:
Exception

onStart

protected void onStart()
Invoked when task starts running.


onFinish

protected void onFinish()
Invoked when task is finished.


onError

protected void onError()
Invoked when task has an error.

Default print stackstrace.


onCanceled

protected void onCanceled()
Invoked when task is canceled.



Copyright 2010 CodeRight, All rights reserved.