Task
Class NetworkTask<ReturnValueType>

java.lang.Object
  extended by Task.AbstractTask
      extended by Task.NetworkTask<ReturnValueType>
Direct Known Subclasses:
RecurringNetworkTask

public class NetworkTask<ReturnValueType>
extends AbstractTask

SwingNetworkTask is a SwingWorker that's optimzed for calling services in the platform and handling mundane error processing, etc. The key difference between this class and SwingTask is that this class is aware of the TaskManager state, and when it is not in ONLINE mode, these tasks do not execute (TaskExecutorIF is not run), they just report they are not in online mode.

When using this class, you can bind your UI to the following properties:

PropertyNames.Status
A string message will be sent as status changes happen
Registering tasks with TaskManager
When an instance of this class is created, it's registered with the manager, using which allows it to monitor running/dead tasks.

Since:
May 30, 2007, 1:33:23 PM
Version:
1.0
Author:
Nazmul Idris

Nested Class Summary
 
Nested classes/interfaces inherited from class Task.AbstractTask
AbstractTask.PropertyNames, AbstractTask.State, AbstractTask.Type
 
Field Summary
protected  TaskExecutorIF<ReturnValueType> _exec
           
protected  javax.swing.SwingWorker<ReturnValueType,java.lang.Void> _swingWorker
           
protected  NetworkTaskHandlerIF<ReturnValueType> _taskHandler
           
protected  boolean testMode_Enabled
           
protected  boolean testMode_isOnline
           
 
Fields inherited from class Task.AbstractTask
_autoShutdownRay, _boundProperties, _boundPropertiesLock, _shutdownCalled, _state, _status, _taskManager
 
Constructor Summary
NetworkTask(TaskManager container, TaskExecutorIF<ReturnValueType> exec, java.lang.String name, java.lang.String descr, AutoShutdownSignals... policy)
           
 
Method Summary
protected  void _createSwingWorker(java.util.concurrent.BlockingQueue gate)
          instantiates the underlying swing worker implementation...
protected  void _openGate(java.util.concurrent.BlockingQueue gate)
          This method releases the thread that's waiting on the BlockingQueue.
 void doShutdown()
          subclass should implement this method as is appropriate for them. this is called when the task is stopped, based on the AutoShutdownSignals policy
 void execute()
          For each invocation, this method creates a new SwingWorker object to perform a background task, and then it uses the provided TasKHandler to notify you of the changes to the task's lifecycle.
protected  void executeAndWait()
          Support for RecurringNetworkTask.
 javax.swing.SwingWorker<ReturnValueType,java.lang.Void> getSwingWorker()
           
 AbstractTask.Type getType()
           
 void setTaskHandler(NetworkTaskHandlerIF<ReturnValueType> handler)
           
 void setTestMode_isOnline(boolean flag)
           
 
Methods inherited from class Task.AbstractTask
_assertIsNotShutdown, _assertNoSwingWorkerCurrentlyActive, addStatusListener, cancel, clearAllStatusListeners, formattedString, getAutoShutdownPolicy, getBoundProperties, getDescription, getName, getState, getStatus, getTaskManager, getUIHook, hasAutoShutdownPolicy, isShutdown, setAutoShutdownPolicy, setDescription, setName, setState, setStatus, setTaskManager, shutdown
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

testMode_Enabled

protected boolean testMode_Enabled

testMode_isOnline

protected boolean testMode_isOnline

_taskHandler

protected NetworkTaskHandlerIF<ReturnValueType> _taskHandler

_exec

protected TaskExecutorIF<ReturnValueType> _exec

_swingWorker

protected javax.swing.SwingWorker<ReturnValueType,java.lang.Void> _swingWorker
Constructor Detail

NetworkTask

public NetworkTask(TaskManager container,
                   TaskExecutorIF<ReturnValueType> exec,
                   java.lang.String name,
                   java.lang.String descr,
                   AutoShutdownSignals... policy)
            throws java.lang.IllegalArgumentException
Throws:
java.lang.IllegalArgumentException
Method Detail

getType

public AbstractTask.Type getType()
Specified by:
getType in class AbstractTask

doShutdown

public void doShutdown()
subclass should implement this method as is appropriate for them. this is called when the task is stopped, based on the AutoShutdownSignals policy

Specified by:
doShutdown in class AbstractTask

setTestMode_isOnline

public void setTestMode_isOnline(boolean flag)

setTaskHandler

public void setTaskHandler(NetworkTaskHandlerIF<ReturnValueType> handler)
                    throws java.lang.IllegalArgumentException
Throws:
java.lang.IllegalArgumentException

getSwingWorker

public javax.swing.SwingWorker<ReturnValueType,java.lang.Void> getSwingWorker()
Specified by:
getSwingWorker in class AbstractTask

execute

public void execute()
             throws TaskException
For each invocation, this method creates a new SwingWorker object to perform a background task, and then it uses the provided TasKHandler to notify you of the changes to the task's lifecycle.

If the task has been shutdown then it can't be executed anymore.

Specified by:
execute in class AbstractTask
Throws:
TaskException

executeAndWait

protected void executeAndWait()
                       throws TaskException
Support for RecurringNetworkTask.

Create a swing worker thread, but wait until the underlying thread is done. This means that the calling thread waits until this underlying swing worker is complete. This is used by RecurringNetworkTask._initScheduledFuture().

What happens when the recurring task is shutdown? Look at RecurringNetworkTask.stopRecurring(). Basically, the task executor is shutdown, which prevents any future tasks from being spawned. What about the task that's already running? By issing a cancel to the _scheduledFuture, it causes InterruptedException to be thrown on the Runnable which is waiting on this method to complete. Hence, in the section in this code where InterruptedException is thrown, it should be taken as a signal to propagate the cancellation of all SwingWorkers that may be currently running (which is why this method would be blocked at the gate). If it's not blocked at the gate, nothing is currently running, so shutdown is trivial.

Throws:
TaskException
See Also:
RecurringNetworkTask.start(), RecurringNetworkTask._initScheduledFuture()

_createSwingWorker

protected void _createSwingWorker(java.util.concurrent.BlockingQueue gate)
instantiates the underlying swing worker implementation...

Parameters:
gate - if it is null, then this is just a regular SwingNetworkTask. if not null, this implies that this is for use with the SwingRecurringNetworkTask, and this gate is meant to block the caller of executeAndWait() blocked until this SwingWorker completes what it's doing (err, ok, interrupted).

_openGate

protected void _openGate(java.util.concurrent.BlockingQueue gate)
This method releases the thread that's waiting on the BlockingQueue. This waiting thread is the one that spawned this SwingWorker thread, from a call to executeAndWait() from the RecurringNetworkTask class.

If the gate is null, then do not do anything. This is an invocation of a regular SwingNetworkTask, not a SwingRecurringNetworkTask.

Parameters:
gate - may be null.


Copyright © 2011. All Rights Reserved.