de.myfoo.commonj.work
Class FooWorkManager

java.lang.Object
  extended by de.myfoo.commonj.util.AbstractManager
      extended by de.myfoo.commonj.work.FooWorkManager
All Implemented Interfaces:
commonj.work.WorkManager

public final class FooWorkManager
extends AbstractManager
implements commonj.work.WorkManager

Implemetation of a WorkManager. The WorkManager is the abstraction for dispatching and monitoring asynchronous work and is a factory for creating application short or long lived Works.

This WorkManager does not support remoting works. However, applications that follow the programming model will work. Serializable Works will be executed within the local JVM but the WorkItem can savely be downcasted to a RemoteWorkItem.

If the scheduled Work is a daemon Work, then the life-cycle of that Work is tied to the application that scheduled it. If the application is stopped, the Work.release() method will be called.

Version:
1.0
Author:
Andreas Keldenich
See Also:
WorkManager

Field Summary
 
Fields inherited from class de.myfoo.commonj.util.AbstractManager
pool
 
Fields inherited from interface commonj.work.WorkManager
IMMEDIATE, INDEFINITE
 
Constructor Summary
FooWorkManager(ThreadPool pool)
          Creates a new instance of FooWorkManager.
FooWorkManager(ThreadPool pool, int maxDaemons)
          Creates a new instance of FooWorkManager.
 
Method Summary
 int getMaxDaemons()
          Getter for maxDaemons
 commonj.work.WorkItem schedule(commonj.work.Work work)
          Dispatches a Work asynchronously.
 commonj.work.WorkItem schedule(commonj.work.Work work, commonj.work.WorkListener wl)
          Dispatches a Work asynchronously.
 void setMaxDaemons(int maxDaemons)
          Setter for maxDaemons
 void shutdown()
          Shutdown the thread pool and daemons.
 boolean waitForAll(java.util.Collection workItems, long timeoutMs)
          Wait for all WorkItems in the collection to finish successfully or otherwise.
 java.util.Collection waitForAny(java.util.Collection workItems, long timeoutMs)
          Wait for any of the WorkItems in the collection to finish.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FooWorkManager

public FooWorkManager(ThreadPool pool)
Creates a new instance of FooWorkManager.

Parameters:
pool - the thread pool to execute work in

FooWorkManager

public FooWorkManager(ThreadPool pool,
                      int maxDaemons)
Creates a new instance of FooWorkManager.

Parameters:
pool - the thread pool to execute work in
maxDaemons - max number of daemons to allow
Method Detail

schedule

public commonj.work.WorkItem schedule(commonj.work.Work work)
                               throws commonj.work.WorkException,
                                      java.lang.IllegalArgumentException
Dispatches a Work asynchronously. The work is dispatched and the method returns immediately. The J2EE context of the caller is used to execute the Work.

At-most-once semantics are provided. If the server fails then the Work will not be executed on restart.

If this FooWorkManager is a pinned one, i.e. one obtained using RemoteWorkItem.getPinnedWorkManager() and that JVM that it represents has failed then a WorkRejectedException will be thrown even if the remote JVM restarts. The pinned WorkManager must be refreshed by using a normal WorkManager and then acquiring a new pinned WorkManager.

Specified by:
schedule in interface commonj.work.WorkManager
Parameters:
work - the Work to execute.
Returns:
the workitem representing the asynchronous work. If the Work is serializable then a RemoteWorkItem is always returned.
Throws:
commonj.work.WorkException - If queuing this up results in an exception then a WorkException is thrown.
java.lang.IllegalArgumentException - thrown if work is a javax.ejb.EnterpriseBean.
See Also:
WorkManager.schedule(commonj.work.Work)

schedule

public commonj.work.WorkItem schedule(commonj.work.Work work,
                                      commonj.work.WorkListener wl)
                               throws commonj.work.WorkException,
                                      java.lang.IllegalArgumentException
Dispatches a Work asynchronously. The work is dispatched and the method returns immediately. The J2EE context of the caller is used to execute the Work.

At-most-once semantics are provided. If the server fails then the Work will not be executed on restart.

The WorkListener methods are called using the J2EE context of the caller as the Work progresses through processing.

If this FooWorkManager is a pinned one, i.e. one obtained using RemoteWorkItem.getPinnedWorkManager() and that JVM that it represents has failed then a WorkRejectedException will be thrown even if the remote JVM restarts. The pinned WorkManager must be refreshed by using a normal WorkManager and then acquiring a new pinned WorkManager.

Specified by:
schedule in interface commonj.work.WorkManager
Parameters:
work - the Work to execute.
wl - can be null or a WorkListener which is used to inform the application of the progress of a Work.
Returns:
The workitem representing the asynchronous work. If the Work is serializable then a RemoteWorkItem is always returned.
Throws:
commonj.work.WorkException - If queuing this up results in an exception then a WorkException is thrown.
java.lang.IllegalArgumentException - thrown if work is a javax.ejb.EnterpriseBean.
See Also:
WorkManager.schedule(commonj.work.Work, commonj.work.WorkListener)

waitForAll

public boolean waitForAll(java.util.Collection workItems,
                          long timeoutMs)
                   throws java.lang.InterruptedException,
                          java.lang.IllegalArgumentException
Wait for all WorkItems in the collection to finish successfully or otherwise. WorkItems from different WorkManagers can be placed in a single collection and waited on together.

The WorkItems collection should not be altered once submitted until the method returns.

Specified by:
waitForAll in interface commonj.work.WorkManager
Parameters:
workItems - the Collection of WorkItem objects to wait for.
timeoutMs - the timout in milliseconds. If this is 0 then this method returns immediately.
Returns:
true if all WorkItems have completed, false if the timeout has expired.
Throws:
java.lang.InterruptedException - thrown if the wait is interrupted.
java.lang.IllegalArgumentException - thrown if workItems is null, any of the objects in the collection are not WorkItems or the timeout_ms is negative.
See Also:
WorkManager.waitForAll(java.util.Collection, long)

waitForAny

public java.util.Collection waitForAny(java.util.Collection workItems,
                                       long timeoutMs)
                                throws java.lang.InterruptedException,
                                       java.lang.IllegalArgumentException
Wait for any of the WorkItems in the collection to finish. If there are no WorkItems in the list then it returns immediately indicating a timeout. WorkItems from different WorkManagers can be placed in a single collection and waited on together.

The WorkItems collection should not be altered once submitted until the method returns.

Specified by:
waitForAny in interface commonj.work.WorkManager
Parameters:
workItems - the Collection of WorkItem objects to wait for.
timeoutMs - the timeout in ms. If this is 0 then the method returns immediately, i.e. does not block.
Returns:
the WorkItems that have completed or an empty Collection if it time out expires before any finished.
Throws:
java.lang.InterruptedException - thrown if the wait is interrupted.
java.lang.IllegalArgumentException - thrown if workItems is null, any of the objects in the collection are not WorkItems or the timeout_ms is negative.
See Also:
WorkManager.waitForAny(java.util.Collection, long)

shutdown

public void shutdown()
Shutdown the thread pool and daemons.

Overrides:
shutdown in class AbstractManager

getMaxDaemons

public int getMaxDaemons()
Getter for maxDaemons

Returns:
Returns the maxDaemons.

setMaxDaemons

public void setMaxDaemons(int maxDaemons)
Setter for maxDaemons

Parameters:
maxDaemons - The maxDaemons to set.