Class QueueManagerBase<T,R>
- java.lang.Object
-
- ch.supertomcat.supertomcatutils.queue.QueueManagerBase<T,R>
-
- Type Parameters:
T- Task TypeR- Task Return Type
public abstract class QueueManagerBase<T,R> extends Object
This class manages the download-slots. It contains also the counters and restrictions for domains. So this class allows a Pic to download or let them wait until a slot is free.
-
-
Field Summary
Fields Modifier and Type Field Description protected booleancheckScheduleTasksFlag for indicating that it should be checked if any tasks from the queue can be executedprotected CompletionService<R>completionServicecompletionServiceprotected Map<String,AtomicInteger>countersCounters to handle max connections per hostprotected List<QueueTask<T,R>>executingTasksTasks which are currently executingprotected org.slf4j.LoggerloggerLoggerprotected intmaxConnectionCountMaximum connection countprotected intmaxConnectionCountPerHostMaximum connection count per hostprotected intopenSlotsOpen Slotsprotected List<T>queueQueueprotected ThreadqueueCompletionThreadQueue Completion Threadprotected QueueTaskFactory<T,R>queueTaskFactoryQueue Task Factoryprotected ThreadschedulerThreadScheduler Threadprotected longsessionBytesBytes since application startedprotected intsessionFilesFiles since application startedprotected booleanstopStop Flagprotected ObjectsyncObjectSynchronization Objectprotected ThreadPoolExecutorthreadPoolThread Pool
-
Constructor Summary
Constructors Constructor Description QueueManagerBase(QueueTaskFactory<T,R> queueTaskFactory, int maxConnectionCount, int maxConnectionCountPerHost)Constructor
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaddTasksToQueue(List<T> tasks)Add tasks to queueprotected voidaddTaskToExecutingTasks(QueueTask<T,R> task)voidaddTaskToQueue(T task)Add task to queueprotected voidapplyMaxConnectionCount()Apply Max Connection Countprotected voidcancelTaskCallables(boolean interruptTaskIfRunning)Cancel executing tasksvoidcancelTasks(boolean cancelAlreadyExecutingTasks)Stop tasks.protected abstract voidcompletedTaskCallable(QueueTask<T,R> task)Called when a task completed, failed or was cancelledintgetMaxConnectionCount()Returns the maxConnectionCountprotected intgetMaxConnectionCount(T task)Get maximum connection count for taskintgetMaxConnectionCountPerHost()Returns the maxConnectionCountPerHostintgetOpenSlots()intgetQueueSize()protected intgetRestrictedCount(String restrictionKey)protected abstract RestrictiongetRestrictionForTask(T task)Restriction for tasklonggetSessionBytes()Returns the sessionBytesintgetSessionFiles()Returns the sessionFilesintgetTaskCount()voidincreaseSessionBytes(long bytes)Increases the bytes since application startedvoidincreaseSessionFiles()Increases the session files since application started by 1voidinit()InitializebooleanisExecutingTasks()protected abstract voidremovedTaskFromQueue(T task, boolean executeFailure)Called when a task was removed from the queue, when the queue was stopped or when a task could not be scheduled for executionprotected voidremoveTaskCallable(Future<R> future)Remove Task Callablesprotected voidremoveTaskCallables()Remove Task CallablesvoidsetMaxConnectionCount(int maxConnectionCount)Sets the maxConnectionCountvoidsetMaxConnectionCountPerHost(int maxConnectionCountPerHost)Sets the maxConnectionCountPerHostvoidstop()Stopprotected voidupdateOpenSlots(boolean taskFinished)Update Open Slots
-
-
-
Field Detail
-
logger
protected org.slf4j.Logger logger
Logger
-
syncObject
protected final Object syncObject
Synchronization Object
-
queueTaskFactory
protected final QueueTaskFactory<T,R> queueTaskFactory
Queue Task Factory
-
executingTasks
protected final List<QueueTask<T,R>> executingTasks
Tasks which are currently executing
-
checkScheduleTasks
protected boolean checkScheduleTasks
Flag for indicating that it should be checked if any tasks from the queue can be executed
-
counters
protected final Map<String,AtomicInteger> counters
Counters to handle max connections per host
-
schedulerThread
protected Thread schedulerThread
Scheduler Thread
-
queueCompletionThread
protected Thread queueCompletionThread
Queue Completion Thread
-
maxConnectionCount
protected int maxConnectionCount
Maximum connection count
-
maxConnectionCountPerHost
protected int maxConnectionCountPerHost
Maximum connection count per host
-
sessionFiles
protected int sessionFiles
Files since application started
-
sessionBytes
protected long sessionBytes
Bytes since application started
-
openSlots
protected int openSlots
Open Slots
-
threadPool
protected ThreadPoolExecutor threadPool
Thread Pool
-
completionService
protected CompletionService<R> completionService
completionService
-
stop
protected boolean stop
Stop Flag
-
-
Constructor Detail
-
QueueManagerBase
public QueueManagerBase(QueueTaskFactory<T,R> queueTaskFactory, int maxConnectionCount, int maxConnectionCountPerHost)
Constructor- Parameters:
queueTaskFactory- Queue Task FactorymaxConnectionCount- Max Connection CountmaxConnectionCountPerHost- Max Connection Count per Host
-
-
Method Detail
-
init
public void init()
Initialize
-
stop
public void stop()
Stop
-
cancelTasks
public void cancelTasks(boolean cancelAlreadyExecutingTasks)
Stop tasks. Clears the queue and cancels are executing tasks- Parameters:
cancelAlreadyExecutingTasks- Flag if already running tasks should be cancelled
-
getMaxConnectionCount
public int getMaxConnectionCount()
Returns the maxConnectionCount- Returns:
- maxConnectionCount
-
getMaxConnectionCountPerHost
public int getMaxConnectionCountPerHost()
Returns the maxConnectionCountPerHost- Returns:
- maxConnectionCountPerHost
-
setMaxConnectionCount
public void setMaxConnectionCount(int maxConnectionCount)
Sets the maxConnectionCount- Parameters:
maxConnectionCount- maxConnectionCount
-
applyMaxConnectionCount
protected void applyMaxConnectionCount()
Apply Max Connection Count
-
setMaxConnectionCountPerHost
public void setMaxConnectionCountPerHost(int maxConnectionCountPerHost)
Sets the maxConnectionCountPerHost- Parameters:
maxConnectionCountPerHost- maxConnectionCountPerHost
-
getSessionFiles
public int getSessionFiles()
Returns the sessionFiles- Returns:
- sessionFiles
-
increaseSessionFiles
public void increaseSessionFiles()
Increases the session files since application started by 1
-
getSessionBytes
public long getSessionBytes()
Returns the sessionBytes- Returns:
- sessionBytes
-
increaseSessionBytes
public void increaseSessionBytes(long bytes)
Increases the bytes since application started- Parameters:
bytes- Bytes
-
getQueueSize
public int getQueueSize()
- Returns:
- Queue size
-
getTaskCount
public int getTaskCount()
- Returns:
- Count of currently executing tasks
-
isExecutingTasks
public boolean isExecutingTasks()
- Returns:
- True if tasks are executing, false otherwise
-
updateOpenSlots
protected void updateOpenSlots(boolean taskFinished)
Update Open Slots- Parameters:
taskFinished- Flag if slots are updated, because a task was finished
-
getOpenSlots
public int getOpenSlots()
- Returns:
- Open slots
-
addTaskToQueue
public void addTaskToQueue(T task)
Add task to queue- Parameters:
task- Task
-
addTasksToQueue
public void addTasksToQueue(List<T> tasks)
Add tasks to queue- Parameters:
tasks- Tasks
-
removedTaskFromQueue
protected abstract void removedTaskFromQueue(T task, boolean executeFailure)
Called when a task was removed from the queue, when the queue was stopped or when a task could not be scheduled for execution- Parameters:
task- TaskexecuteFailure- True if task could not be scheduled for execution, false otherwise
-
addTaskToExecutingTasks
protected void addTaskToExecutingTasks(QueueTask<T,R> task)
- Parameters:
task- Task
-
cancelTaskCallables
protected void cancelTaskCallables(boolean interruptTaskIfRunning)
Cancel executing tasks- Parameters:
interruptTaskIfRunning- True if running tasks should be interrupted, false otherwise
-
removeTaskCallables
protected void removeTaskCallables()
Remove Task Callables
-
removeTaskCallable
protected void removeTaskCallable(Future<R> future)
Remove Task Callables- Parameters:
future- Future
-
completedTaskCallable
protected abstract void completedTaskCallable(QueueTask<T,R> task)
Called when a task completed, failed or was cancelled- Parameters:
task- Task
-
getRestrictionForTask
protected abstract Restriction getRestrictionForTask(T task)
Restriction for task- Parameters:
task- Task- Returns:
- Restriction for task
-
getRestrictedCount
protected int getRestrictedCount(String restrictionKey)
- Parameters:
restrictionKey- Restriction Key- Returns:
- Current Restricted Count
-
getMaxConnectionCount
protected int getMaxConnectionCount(T task)
Get maximum connection count for task- Parameters:
task- Task- Returns:
- Maximum connection count for task
-
-