FileDocCategorySizeDatePackage
ThreadPoolExecutorPipeline.javaAPI DocGlassfish v2 API11000Fri May 04 22:37:06 BST 2007com.sun.enterprise.web.connector.grizzly

ThreadPoolExecutorPipeline

public class ThreadPoolExecutorPipeline extends Object implements RejectedExecutionHandler, Pipeline
A wrapper around an ThreadPoolExecutor. This thread pool is bounded by an ArrayBlockingQueue
author
Jean-Francois Arcand

Fields Summary
private int
waitingThreads
The number of thread waiting for a Task
private int
maxThreads
The maximum number of Thread
private int
minThreads
The minimum numbers of WorkerThread
private int
port
The port used.
private int
threadCount
The number of WorkerThread
private String
name
The name of this Pipeline
private int
priority
The Thread Priority
private boolean
isStarted
Has the pipeline already started
private ThreadPoolExecutor
workerThreads
ExecutorService wrapped by this pipeline.
private ArrayBlockingQueue
arrayBlockingQueue
Connection queue
private int
maxQueueSizeInBytes
Maximum pending connection before refusing requests.
private int
queueSizeInBytes
maximum size of the connection queue, in bytes.
protected PipelineStatistic
pipelineStat
The PipelineStatistic objects used when gathering statistics.
Constructors Summary
Methods Summary
public voidaddTask(Task task)
Add an object to this pipeline

        if (workerThreads.getQueue().size() > maxQueueSizeInBytes ){
            task.cancelTask("Maximum Connections Reached: " 
                            + pipelineStat.getQueueSizeInBytes()
                            + " -- Retry later", HtmlHelper.OK);
            task.getSelectorThread().returnTask(task);
            return;                                               
        }       
        workerThreads.execute((Runnable)task);
                
        if ( pipelineStat != null) {
            pipelineStat.gather(size());
        }        
    
public booleanexpireKey(java.nio.channels.SelectionKey key)
Invoked when the SelectorThread is about to expire a SelectionKey.

return
true if the SelectorThread should expire the SelectionKey, false if not.

       return true; 
    
public intgetCurrentThreadCount()
Return the current number of threads used.

        return workerThreads.getPoolSize() ;
    
public intgetCurrentThreadsBusy()
Return the curent number of threads that are currently processing a task.

        return workerThreads.getActiveCount();
    
public intgetMaxSpareThreads()
Return the maximum spare thread.

        return getWaitingThread();
    
public intgetMaxThreads()
Return the number of threads used by this pipeline.

        return maxThreads;
    
public intgetMinSpareThreads()
Return the minimum spare thread.

        return 0;
    
public java.lang.StringgetName()
Return the name of this Pipeline

return
the name of this Pipeline

        return name+port;
    
public PipelineStatisticgetPipelineStatistic()
Return the PipelineStatistic object used to gather statistic;

        return pipelineStat;
    
public intgetQueueSizeInBytes()
Get the maximum pending connection this Pipeline can handle.

        return maxQueueSizeInBytes;
    
public TaskgetTask()
Return a Task object available in the pipeline.

        return null;
    
public intgetWaitingThread()
Return the number of waiting threads.

        return workerThreads.getPoolSize() - workerThreads.getActiveCount();
    
public voidinitPipeline()
Init the Pipeline by initializing the required ThreadPoolExecutor.

    // ------------------------------------------------ Lifecycle ------------/
    
                  
      
        
        if (isStarted){
            return;
        }
        isStarted = true;
        arrayBlockingQueue = 
                        new ArrayBlockingQueue<Runnable>(maxQueueSizeInBytes, true);
        
        workerThreads = new ThreadPoolExecutor(
                               maxThreads,
                               maxThreads,
                               0L,
                               TimeUnit.MILLISECONDS,
                               arrayBlockingQueue,
                               new GrizzlyThreadFactory(name,port,priority),
                               this);
    
public booleaninterruptThread(long threadID)
Interrupt the Thread using it thread id

        return ((GrizzlyThreadFactory)workerThreads.getThreadFactory())
            .interruptThread(threadID);
    
public voidrejectedExecution(java.lang.Runnable r, java.util.concurrent.ThreadPoolExecutor executor)
When the maxQueueSizeInBytesConnection is reached, terminate Task

        Task task = (Task)r;
        task.cancelTask("Maximum Connections Reached -- Retry later", 
                        HtmlHelper.OK);
        task.getSelectorThread().returnTask(task);
    
public voidsetMaxThreads(int maxThreads)
Set the number of threads used by this pipeline.

        this.maxThreads = maxThreads;
    
public voidsetMinSpareThreads(int minSpareThreads)
Set the minimum space thread this Pipeline can handle.

    
public voidsetMinThreads(int minThreads)
Set the minimum thread this Pipeline will creates when initializing.

param
minThreads the minimum number of threads.

        this.minThreads = minThreads;
    
public voidsetName(java.lang.String name)
Set the name of this Pipeline

        this.name = name;
    
public voidsetPipelineStatistic(PipelineStatistic pipelineStatistic)
Set the PipelineStatistic object used to gather statistic;

        this.pipelineStat = pipelineStatistic;
    
public voidsetPort(int port)
Set the port used by this Pipeline

param
port the port used by this Pipeline

        this.port = port;
    
public voidsetPriority(int priority)
Set the thread priority of the Pipeline

        this.priority = priority;
    
public voidsetQueueSizeInBytes(int maxQueueSizeInBytes)
Set the maximum pending connection this Pipeline can handle.

        this.maxQueueSizeInBytes = maxQueueSizeInBytes;
        if ( pipelineStat != null )
            pipelineStat.setQueueSizeInBytes(maxQueueSizeInBytes);
    
public voidsetThreadsIncrement(int threadsIncrement)

        ; // Not Supported
    
public voidsetThreadsTimeout(int threadsTimeout)

        ; // Not Supported
    
public intsize()
Returns the number of tasks in this Pipeline.

return
Number of tasks in this Pipeline.

        return workerThreads.getQueue().size();
    
public voidstartPipeline()
Start the Pipeline

        if (isStarted){
            return;
        }
        ; // Do nothing
    
public voidstopPipeline()
Stop the Pipeline

        if (!isStarted){
            return;
        }
        isStarted = false;
        workerThreads.shutdown();
    
public java.lang.StringtoString()

       return "name: " + name + " maxThreads: " + maxThreads 
                + " minThreads:" + minThreads;