Package it.tidalwave.image.processor
Class ImagingTaskProcessor
- java.lang.Object
-
- it.tidalwave.image.processor.ImagingTaskProcessor
-
- Direct Known Subclasses:
LocalImagingTaskProcessor
public abstract class ImagingTaskProcessor extends java.lang.Object- Author:
- Fabrizio Giudici
-
-
Field Summary
Fields Modifier and Type Field Description protected intfreeWorkersThe current number of free workers.protected java.lang.ObjectlockA lock used to manipulate the inner state.protected static intmaxWorkersThe max number of workers.
-
Constructor Summary
Constructors Modifier Constructor Description protectedImagingTaskProcessor()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaddListener(ImagingTaskProcessorListener listener)<T extends ImagingTask>
java.util.Collection<T>cancellPendingTasks(java.lang.Class<T> taskClass)Cancels all the pending tasks of the given type.protected voidchangeFreeWorkerCount(int delta)intgetCompletedTaskCount(java.lang.Class<? extends ImagingTask> taskClass)Returns the number of completed tasks of the given class.static ImagingTaskProcessorgetInstance()Gets the singleton instance of the processor.intgetMaxWorkers()protected ImagingTaskgetNextTask(java.io.Serializable workerId, boolean remoteExecution)Returns the next pending task to execute.intgetPendingTaskCount(java.lang.Class<? extends ImagingTask> taskClass)Returns the number of pending tasks of the given class.intgetRunningTaskCount(java.lang.Class<? extends ImagingTask> taskClass)Returns the number of running tasks of the given class.StatisticsgetStatistics()abstract intgetWorkerCount()abstract java.util.Collection<java.io.Serializable>getWorkerIds()abstract booleanhasFileAccess()Returns true if the tasks will be executed in a distributed context with the support of a distributed file system - that is, if each task is guaranteed to have a File access to the same set of data.abstract booleanisDistributed()Returns true if the tasks will be executed in a distributed context (i.e.protected voidnotifyTaskCompleted(ImagingTask task)Notifies that a task has been completed.<T extends ImagingTask>
TpopCompletedTask(java.lang.Class<T> taskClass)Pops a completed task out of the list.voidpost(ImagingTask task)Posts a new task into the queue.voidpost(java.util.Collection<? extends ImagingTask> tasks)Posts a new task into the queue.voidpostWithPriority(ImagingTask task)voidpostWithPriority(java.util.Collection<? extends ImagingTask> tasks)booleanprocessingResourcesAvailable()voidremoveListener(ImagingTaskProcessorListener listener)static voidsetDefault(java.lang.Class<? extends ImagingTaskProcessor> defaultClass)Sets the default implementation of the ImagingTaskProcessorstatic voidsetMaxWorkers(int maxWorkers)Sets a limit to the number of concurrent workers.
-
-
-
Method Detail
-
getInstance
public static ImagingTaskProcessor getInstance()
Gets the singleton instance of the processor.- Returns:
- the ImagingTaskProcessor
-
setDefault
public static void setDefault(java.lang.Class<? extends ImagingTaskProcessor> defaultClass)
Sets the default implementation of the ImagingTaskProcessor- Parameters:
defaultClass- the implementation class
-
setMaxWorkers
public static void setMaxWorkers(int maxWorkers)
Sets a limit to the number of concurrent workers.- Parameters:
maxWorkers- the max. number of workers
-
getMaxWorkers
public int getMaxWorkers()
-
getWorkerCount
public abstract int getWorkerCount()
-
getWorkerIds
public abstract java.util.Collection<java.io.Serializable> getWorkerIds()
-
isDistributed
public abstract boolean isDistributed()
Returns true if the tasks will be executed in a distributed context (i.e. with different physical nodes - in single-node, multi-core contexts this method returns false.
-
hasFileAccess
public abstract boolean hasFileAccess()
Returns true if the tasks will be executed in a distributed context with the support of a distributed file system - that is, if each task is guaranteed to have a File access to the same set of data. Or if the context is a single-node, multi-core system, which of course has a single filesystem.
-
addListener
public void addListener(ImagingTaskProcessorListener listener)
-
removeListener
public void removeListener(ImagingTaskProcessorListener listener)
-
processingResourcesAvailable
public boolean processingResourcesAvailable()
-
post
public void post(java.util.Collection<? extends ImagingTask> tasks)
Posts a new task into the queue. The task will be scheduled later as soon there are favorable conditions. This method returns immediately.- Parameters:
task- the ImagingTask to post
-
post
public void post(ImagingTask task)
Posts a new task into the queue. The task will be scheduled later as soon there are favorable conditions. This method returns immediately.- Parameters:
task- the ImagingTask to post
-
postWithPriority
public void postWithPriority(java.util.Collection<? extends ImagingTask> tasks)
- Parameters:
task- the ImagingTask to post
-
postWithPriority
public void postWithPriority(ImagingTask task)
- Parameters:
task- the ImagingTasks to post
-
cancellPendingTasks
public final <T extends ImagingTask> java.util.Collection<T> cancellPendingTasks(java.lang.Class<T> taskClass)
Cancels all the pending tasks of the given type. This method returns immediately. Tasks in running state will be completed.- Parameters:
taskClass- the kind of task to remove (null means all)- Returns:
- the list of completed tasks
-
getPendingTaskCount
public int getPendingTaskCount(java.lang.Class<? extends ImagingTask> taskClass)
Returns the number of pending tasks of the given class.- Parameters:
taskClass- the class of the task (null for any class)- Returns:
- the task count
-
getRunningTaskCount
public int getRunningTaskCount(java.lang.Class<? extends ImagingTask> taskClass)
Returns the number of running tasks of the given class.- Parameters:
taskClass- the class of the task (null for any class)- Returns:
- the task count
-
getCompletedTaskCount
public int getCompletedTaskCount(java.lang.Class<? extends ImagingTask> taskClass)
Returns the number of completed tasks of the given class. This number is decreased as popCompletedTask() is called.- Parameters:
taskClass- the class of the task (null for any class)- Returns:
- the task count
-
popCompletedTask
public <T extends ImagingTask> T popCompletedTask(java.lang.Class<T> taskClass)
Pops a completed task out of the list. This will decrease the count of completed task. This operation is usually performed by the controller to consume the task result and eventually schedule a new task.- Parameters:
taskClass- the class of the task- Returns:
- the completed task
- Throws:
java.util.NoSuchElementException- if no tasks of the given class is available
-
getStatistics
public Statistics getStatistics()
-
changeFreeWorkerCount
protected void changeFreeWorkerCount(int delta)
-
getNextTask
protected final ImagingTask getNextTask(java.io.Serializable workerId, boolean remoteExecution)
Returns the next pending task to execute. This method blocks until a pending task is available.- Returns:
- the task to execute
-
notifyTaskCompleted
protected final void notifyTaskCompleted(ImagingTask task)
Notifies that a task has been completed. The task is removed from the list of running tasks and added into the list of completed tasks. If an ImagingTaskProcessorController has been specified, it is notified too.
This method is able to filter out duplicated tasks (that could occur with some distributed implementations.
It is important that this method blocks until the controller has been invoked - otherwise e.g. the Computer Server implementation could fail, for instance if there are no running tasks and the controller has not been able to post new tasks yet. In this circumstances, the facility erroneously thinks that everything is over and quits.
- Parameters:
task- the completed task
-
-