Fields Summary |
---|
protected int | waitingThreadsThe number of thread waiting for a Task |
protected int | maxThreadsThe maximum number of Thread |
protected int | minThreadsThe minimum numbers of WorkerThreadImpl |
protected int | minSpareThreadsThe minimum numbers of spare WorkerThreadImpl |
protected int | portThe port used. |
protected int | threadCountThe number of WorkerThreadImpl |
protected String | nameThe name of this Pipeline |
protected int | priorityThe Thread Priority |
protected boolean | isStartedHas the pipeline already started |
protected transient WorkerThreadImpl[] | workerThreadsWorkerThreadImpl amanged by this pipeline. |
protected int | maxQueueSizeInBytesMaximum pending connection before refusing requests. |
protected int | threadsIncrementThe increment number used when adding new thread. |
protected int | threadsTimeoutThe request times out during transaction. |
protected transient PipelineStatistic | pipelineStatThe PipelineStatistic objects used when gathering statistics. |
Methods Summary |
---|
public synchronized void | addTask(Task task)Add an object to this pipeline
boolean rejected = false;
int queueSize = size();
if ( maxQueueSizeInBytes != -1 && maxQueueSizeInBytes <= queueSize){
task.cancelTask("Maximum Connections Reached: "
+ maxQueueSizeInBytes + " -- Retry later",
"HTTP/1.1 503 Service Unavailable");
task.getSelectorThread().returnTask(task);
return;
}
addLast(task);
notify();
// Create worker threads if we know we will run out of them
if (threadCount < maxThreads && waitingThreads < (queueSize + 1)){
int left = maxThreads - threadCount;
if (threadsIncrement > left){
threadsIncrement = left;
}
increaseWorkerThread(threadsIncrement,true);
}
|
public boolean | expireKey(java.nio.channels.SelectionKey key)Invoked when the SelectorThread is about to expire a SelectionKey.
return true;
|
public synchronized int | getCurrentThreadCount()
return threadCount;
|
public synchronized int | getCurrentThreadsBusy()Return the curent number of threads that are currently processing
a task.
return (threadCount - waitingThreads);
|
public synchronized int | getMaxSpareThreads()Return the maximum spare thread.
return maxThreads;
|
public synchronized int | getMaxThreads()Return the number of threads used by this pipeline.
return maxThreads;
|
public synchronized int | getMinSpareThreads()Return the minimum spare thread.
return minSpareThreads;
|
public synchronized java.lang.String | getName()Return the name of this Pipeline
return name+port;
|
public PipelineStatistic | getPipelineStatistic()Return the PipelineStatistic object used
to gather statistic;
return pipelineStat;
|
public synchronized int | getQueueSizeInBytes()Get the maximum pending connection this Pipeline
can handle.
return maxQueueSizeInBytes;
|
public synchronized Task | getTask()Return a Task object available in the pipeline.
All Threads will synchronize on that method
if (size() - waitingThreads <= 0) {
try {
waitingThreads++;
wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupted();
}
waitingThreads--;
}
if (pipelineStat != null) {
pipelineStat.gather(size());
}
return poll();
|
public synchronized int | getTaskQueuedCount()The number of Task currently queued
return size();
|
public synchronized int | getWaitingThread()Return the number of waiting threads.
return waitingThreads;
|
protected void | increaseWorkerThread(int increment, boolean startThread)Create new WorkerThreadImpl . This method must be invoked
from a synchronized block.
WorkerThreadImpl workerThread;
int currentCount = threadCount;
int increaseCount = threadCount + increment;
for (int i=currentCount; i < increaseCount; i++){
workerThread = new WorkerThreadImpl(this,
name + "WorkerThread-" + port + "-" + i);
workerThread.setPriority(priority);
if (startThread)
workerThread.start();
workerThreads[i] = workerThread;
threadCount++;
}
|
public synchronized void | initPipeline()Init the Pipeline by initializing the required
WorkerThreadImpl . Default value is 10
if (minThreads > maxThreads) {
minThreads = maxThreads;
}
workerThreads = new WorkerThreadImpl[maxThreads];
increaseWorkerThread(minThreads, false);
|
public synchronized boolean | interruptThread(long threadID)Interrupt the Thread using it thread id
ThreadGroup threadGroup = workerThreads[0].getThreadGroup();
Thread[] threads = new Thread[threadGroup.activeCount()];
threadGroup.enumerate(threads);
for (Thread thread: threads){
if ( thread != null && thread.getId() == threadID ){
if ( Thread.State.RUNNABLE != thread.getState()){
try{
thread.interrupt();
return true;
} catch (Throwable t){
; // Swallow any exceptions.
}
}
}
}
return false;
|
public boolean | isEmpty()Return true if the size of this ArrayList
minus the current waiting threads is lower than zero.
return (size() - waitingThreads <= 0);
|
public synchronized void | setMaxThreads(int maxThreads)Set the number of threads used by this pipeline.
this.maxThreads = maxThreads;
|
public synchronized void | setMinSpareThreads(int minSpareThreads)Set the minimum space thread this Pipeline can handle.
this.minSpareThreads = minSpareThreads;
|
public synchronized void | setMinThreads(int minThreads)Set the minimum thread this Pipeline will creates
when initializing.
this.minThreads = minThreads;
|
public synchronized void | setName(java.lang.String name)Set the name of this Pipeline
this.name = name;
|
public void | setPipelineStatistic(PipelineStatistic pipelineStatistic)Set the PipelineStatistic object used
to gather statistic;
this.pipelineStat = pipelineStatistic;
|
public synchronized void | setPort(int port)Set the port used by this Pipeline
this.port = port;
|
public synchronized void | setPriority(int priority)Set the thread priority of the Pipeline
this.priority = priority;
|
public synchronized void | setQueueSizeInBytes(int maxQueueSizeInBytesCount)Set the maximum pending connection this Pipeline
can handle.
this.maxQueueSizeInBytes = maxQueueSizeInBytesCount;
|
public synchronized void | setThreadsIncrement(int threadsIncrement)Set the number the Pipeline will use when increasing the
thread pool
this.threadsIncrement = threadsIncrement;
|
public synchronized void | setThreadsTimeout(int threadsTimeout)Set the timeout value a thread will use to times out the request.
this.threadsTimeout = threadsTimeout;
|
public synchronized void | startPipeline()Start the Pipeline and all associated
WorkerThreadImpl
if (!isStarted) {
for (int i=0; i < minThreads; i++){
workerThreads[i].start();
}
isStarted = true;
}
|
public synchronized void | stopPipeline()Stop the Pipeline and all associated
WorkerThreadImpl
if (isStarted) {
for (int i=0; i < threadCount; i++){
workerThreads[i].terminate();
}
isStarted = false;
}
notifyAll();
|
public java.lang.String | toString()
return "name: " + name + " maxThreads: " + maxThreads
+ " type: " + this.getClass().getName();
|