FileDocCategorySizeDatePackage
LinkedListPipeline.javaAPI DocGlassfish v2 API13971Fri Jun 01 07:52:54 BST 2007com.sun.enterprise.web.connector.grizzly

LinkedListPipeline

public class LinkedListPipeline extends LinkedList implements Pipeline
Internal FIFO used by the Worker Threads to pass information between Task objects.
author
Jean-Francois Arcand

Fields Summary
protected int
waitingThreads
The number of thread waiting for a Task
protected int
maxThreads
The maximum number of Thread
protected int
minThreads
The minimum numbers of WorkerThreadImpl
protected int
minSpareThreads
The minimum numbers of spare WorkerThreadImpl
protected int
port
The port used.
protected int
threadCount
The number of WorkerThreadImpl
protected String
name
The name of this Pipeline
protected int
priority
The Thread Priority
protected boolean
isStarted
Has the pipeline already started
protected transient WorkerThreadImpl[]
workerThreads
WorkerThreadImpl amanged by this pipeline.
protected int
maxQueueSizeInBytes
Maximum pending connection before refusing requests.
protected int
threadsIncrement
The increment number used when adding new thread.
protected int
threadsTimeout
The request times out during transaction.
protected transient PipelineStatistic
pipelineStat
The PipelineStatistic objects used when gathering statistics.
Constructors Summary
public LinkedListPipeline()

    
    // ------------------------------------------------------- Constructor -----/
    
     
        super();
    
public LinkedListPipeline(int maxThreads, int minThreads, String name, int port, int priority)

                        
        this.maxThreads = maxThreads;
        this.port = port;
        this.name = name;
        this.minThreads = minThreads;
        this.priority = priority;
        
        if ( minThreads < minSpareThreads )
            minSpareThreads = minThreads;
        
    
public LinkedListPipeline(int maxThreads, int minThreads, String name, int port)

                        
        this(maxThreads,minThreads,name,port,Thread.NORM_PRIORITY);
    
Methods Summary
public synchronized voidaddTask(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 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 synchronized intgetCurrentThreadCount()

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

        return (threadCount - waitingThreads);
    
public synchronized intgetMaxSpareThreads()
Return the maximum spare thread.

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

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

        return minSpareThreads;
    
public synchronized 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 synchronized intgetQueueSizeInBytes()
Get the maximum pending connection this Pipeline can handle.

        return maxQueueSizeInBytes;
    
public synchronized TaskgetTask()
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 intgetTaskQueuedCount()
The number of Task currently queued

return
number of queued connections

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

        return waitingThreads;
    
protected voidincreaseWorkerThread(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 voidinitPipeline()
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 booleaninterruptThread(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 booleanisEmpty()
Return true if the size of this ArrayList minus the current waiting threads is lower than zero.

        return  (size() - waitingThreads <= 0);
    
public synchronized voidsetMaxThreads(int maxThreads)
Set the number of threads used by this pipeline.

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

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

param
minThreads the minimum number of threads.

        this.minThreads = minThreads;
    
public synchronized 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 synchronized voidsetPort(int port)
Set the port used by this Pipeline

param
port the port used by this Pipeline

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

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

        this.maxQueueSizeInBytes = maxQueueSizeInBytesCount;
    
public synchronized voidsetThreadsIncrement(int threadsIncrement)
Set the number the Pipeline will use when increasing the thread pool

        this.threadsIncrement = threadsIncrement;
    
public synchronized voidsetThreadsTimeout(int threadsTimeout)
Set the timeout value a thread will use to times out the request.

        this.threadsTimeout = threadsTimeout;
    
public synchronized voidstartPipeline()
Start the Pipeline and all associated WorkerThreadImpl

        if (!isStarted) {
            for (int i=0; i < minThreads; i++){
                workerThreads[i].start();
            }
            isStarted = true;
        }
    
public synchronized voidstopPipeline()
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.StringtoString()

       return "name: " + name + " maxThreads: " + maxThreads 
                + " type: " + this.getClass().getName();