uws.job.manager
Class QueuedExecutionManager

java.lang.Object
  extended by uws.job.manager.QueuedExecutionManager
All Implemented Interfaces:
java.io.Serializable, ExecutionManager

public class QueuedExecutionManager
extends java.lang.Object
implements ExecutionManager

Implementation of the interface ExecutionManager which lets managing an execution queue in function of a maximum number of running jobs: if there are more running jobs than a given number, the jobs to execute are put in the queue until a running job stops. The order of queued jobs are preserved: it is implemented by a FIFO queue.

Note: By overriding isReadyForExecution(AbstractJob) you can easily change the condition which lets deciding when to put a job in the queue. By default: when there are more running jobs than nbMaxRunningJobs.

Version:
02/2011
Author:
Grégory Mantelet (CDS)
See Also:
Serialized Form

Field Summary
protected  int nbMaxRunningJobs
          The maximum number of running jobs.
static int NO_QUEUE
          The value of nbMaxRunningJobs which indicates that there is no queue.
protected  java.util.Vector<AbstractJob> queuedJobs
          List of queued jobs.
protected  java.util.Map<java.lang.String,AbstractJob> runningJobs
          List of running jobs.
 
Constructor Summary
QueuedExecutionManager()
          Builds an execution manager without queue.
QueuedExecutionManager(int maxRunningJobs)
          Builds an execution manager with a queue.
 
Method Summary
 ExecutionPhase execute(AbstractJob jobToExecute)
          By default a job is executed if there are not yet more than getMaxRunningJobs() running jobs.
 int getMaxRunningJobs()
          Gets the maximum number of running jobs.
 int getNbQueuedJobs()
          Gets the total number of queued jobs.
 int getNbRunningJobs()
          Gets the total number of running jobs.
 java.util.Iterator<AbstractJob> getQueuedJobs()
          Gets the list of queued jobs.
 java.util.Iterator<AbstractJob> getRunningJobs()
          Gets the list of running jobs.
 boolean hasQueue()
          Indicates whether this execution manager has a queue or not.
 boolean isReadyForExecution(AbstractJob jobToExecute)
          Tells whether the given job can start immediately its execution or whether it must be put in the queue.
 void refresh()
          Removes the first queued job(s) from the queue and executes it (them) ONLY IF it (they) can be executed (see isReadyForExecution(AbstractJob)).
 void remove(AbstractJob jobToRemove)
          Removes the job from this manager whatever is its current execution phase.
 void setMaxRunningJobs(int maxRunningJobs)
          Sets the maximum number of running jobs.
 void setNoQueue()
          Lets indicating that no more jobs must be put in the queue.
 void sync()
          This method is called just after a de-serialization.
 void update(AbstractJob job)
          Puts the given job into the appropriate list of jobs: if EXECUTING, the job must be put in the list of running jobs, but if QUEUED, it must be put into the queue.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

runningJobs

protected transient java.util.Map<java.lang.String,AbstractJob> runningJobs
List of running jobs.


queuedJobs

protected transient java.util.Vector<AbstractJob> queuedJobs
List of queued jobs.


nbMaxRunningJobs

protected int nbMaxRunningJobs
The maximum number of running jobs.


NO_QUEUE

public static final int NO_QUEUE
The value of nbMaxRunningJobs which indicates that there is no queue.

See Also:
Constant Field Values
Constructor Detail

QueuedExecutionManager

public QueuedExecutionManager()
Builds an execution manager without queue.

See Also:
sync()

QueuedExecutionManager

public QueuedExecutionManager(int maxRunningJobs)
Builds an execution manager with a queue. The number of executing jobs is limited by the given value (if positive and different from 0).

Parameters:
maxRunningJobs - The maximum number of running jobs (must be > 0 to have a queue).
See Also:
sync()
Method Detail

sync

public final void sync()
Description copied from interface: ExecutionManager

This method is called just after a de-serialization. It resets all: just after a de-serialization there is no running jobs so all jobs lists are cleared.

Specified by:
sync in interface ExecutionManager

getRunningJobs

public final java.util.Iterator<AbstractJob> getRunningJobs()
Description copied from interface: ExecutionManager
Gets the list of running jobs.

Specified by:
getRunningJobs in interface ExecutionManager
Returns:
An iterator on the running jobs.

getNbRunningJobs

public final int getNbRunningJobs()
Description copied from interface: ExecutionManager
Gets the total number of running jobs.

Specified by:
getNbRunningJobs in interface ExecutionManager
Returns:
The number of running jobs.

getQueuedJobs

public final java.util.Iterator<AbstractJob> getQueuedJobs()
Description copied from interface: ExecutionManager
Gets the list of queued jobs.

Specified by:
getQueuedJobs in interface ExecutionManager
Returns:
An iterator on the queued jobs.

getNbQueuedJobs

public final int getNbQueuedJobs()
Description copied from interface: ExecutionManager
Gets the total number of queued jobs.

Specified by:
getNbQueuedJobs in interface ExecutionManager
Returns:
The number of queued jobs.

setNoQueue

public void setNoQueue()
Description copied from interface: ExecutionManager
Lets indicating that no more jobs must be put in the queue. Consequently all current queued jobs have to start immediately.

Specified by:
setNoQueue in interface ExecutionManager

hasQueue

public boolean hasQueue()
Description copied from interface: ExecutionManager
Indicates whether this execution manager has a queue or not.

Specified by:
hasQueue in interface ExecutionManager
Returns:
true if this manager has a queue, false otherwise.

getMaxRunningJobs

public final int getMaxRunningJobs()
Gets the maximum number of running jobs.

Returns:
The maximum number of running jobs.

setMaxRunningJobs

public void setMaxRunningJobs(int maxRunningJobs)
                       throws UWSException

Sets the maximum number of running jobs.

Note: If the new maximum number of running jobs is increasing the list of running jobs is immediately updated BUT NOT IF it is decreasing (that is to say, running jobs will not be interrupted to be put in the queue, they continue to run) !

Parameters:
maxRunningJobs - The new maximum number of running jobs (NO_QUEUE or a negative value means no maximum number of running jobs: there will be no queue any more).
Throws:
UWSException - If there is an error while updating the list of running jobs (in other words if some queued jobs can not be executed).
See Also:
refresh()

refresh

public final void refresh()
                   throws UWSException

Removes the first queued job(s) from the queue and executes it (them) ONLY IF it (they) can be executed (see isReadyForExecution(AbstractJob)).

Note: Nothing is done if there is no queue.

Specified by:
refresh in interface ExecutionManager
Throws:
UWSException - If there is an error during the phase transition of one or more jobs.
See Also:
sync(), hasQueue(), isReadyForExecution(AbstractJob), AbstractJob.start(boolean)

execute

public final ExecutionPhase execute(AbstractJob jobToExecute)
                             throws UWSException

By default a job is executed if there are not yet more than getMaxRunningJobs() running jobs.

Specified by:
execute in interface ExecutionManager
Parameters:
jobToExecute - The job to execute.
Returns:
The resulting execution phase of the given job (EXECUTING or QUEUED or null if the given job is null).
Throws:
UWSException - If there is an error while changing the execution phase of the given job or if the job is already finished.
See Also:
refresh(), AbstractJob.isRunning(), hasQueue(), isReadyForExecution(AbstractJob), AbstractJob.start(boolean), AbstractJob.setPhase(ExecutionPhase)

isReadyForExecution

public final boolean isReadyForExecution(AbstractJob jobToExecute)
Description copied from interface: ExecutionManager

Tells whether the given job can start immediately its execution or whether it must be put in the queue.

Specified by:
isReadyForExecution in interface ExecutionManager
Parameters:
jobToExecute - The job to execute.
Returns:
true if the job can start immediately, false otherwise.

update

public final void update(AbstractJob job)
                  throws UWSException
Description copied from interface: ExecutionManager

Puts the given job into the appropriate list of jobs: if EXECUTING, the job must be put in the list of running jobs, but if QUEUED, it must be put into the queue. In another case, nothing is done except refreshing the list of running jobs.

Specified by:
update in interface ExecutionManager
Parameters:
job - The job to "add" to this manager.
Throws:
UWSException - If there is an error while refreshing the list of running jobs or if any other error occurs.

remove

public final void remove(AbstractJob jobToRemove)
                  throws UWSException
Description copied from interface: ExecutionManager
Removes the job from this manager whatever is its current execution phase.

Specified by:
remove in interface ExecutionManager
Parameters:
jobToRemove - The job to remove.
Throws:
UWSException - If there is an error while refreshing the list of running jobs or if any other error occurs.