Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License.  You can
  * obtain a copy of the License at
  * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
  * or packager/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  *
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at packager/legal/LICENSE.txt.
  *
  * GPL Classpath Exception:
  * Oracle designates this particular file as subject to the "Classpath"
  * exception as provided by Oracle in the GPL Version 2 section of the License
  * file that accompanied this code.
  *
  * Modifications:
  * If applicable, add the following below the License Header, with the fields
  * enclosed by brackets [] replaced by your own identifying information:
  * "Portions Copyright [year] [name of copyright owner]"
  *
  * Contributor(s):
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
  */
 
 package com.sun.enterprise.web.connector.grizzly;
 
A wrapper around an ThreadPoolExecutor. This thread pool is bounded by an ArrayBlockingQueue

Author(s):
Jean-Francois Arcand
 
 public class ThreadPoolExecutorPipeline implements Pipeline,
                                                    RejectedExecutionHandler{
    

    
The number of thread waiting for a Task
 
     private int waitingThreads = 0;
    
    
    
The maximum number of Thread
 
     private int maxThreads = 20;
    

    
The minimum numbers of WorkerThread
 
     private int minThreads = 10;

    
    
The port used.
 
     private int port = 8080;
    

    
The number of WorkerThread
 
     private int threadCount =0;
    

    
The name of this Pipeline
 
     private String name;
    
    
    
The Thread Priority
 
     private int priority = .;
    
    
    
Has the pipeline already started
    private boolean isStarted = false
    
    
    
ExecutorService wrapped by this pipeline.
    private ThreadPoolExecutor workerThreads;
    
   
    
Connection queue
    private ArrayBlockingQueue<RunnablearrayBlockingQueue;
    
    
    
Maximum pending connection before refusing requests.
    private int maxQueueSizeInBytes = -1;
    
    
    
maximum size of the connection queue, in bytes.
    private int queueSizeInBytes = 4096;
    
    
    
The PipelineStatistic objects used when gathering statistics.
    protected PipelineStatistic pipelineStat;
    // ------------------------------------------------ Lifecycle ------------/
    
    
Init the Pipeline by initializing the required ThreadPoolExecutor.
    public void initPipeline(){
        
        if (){
            return;
        }
         = true;
         = 
                        new ArrayBlockingQueue<Runnable>(true);
        
         = new ThreadPoolExecutor(
                               ,
                               ,
                               0L,
                               .,
                               ,
                               new GrizzlyThreadFactory(,,),
                               this);
    }

    
Start the Pipeline
    public void startPipeline(){
        if (){
            return;
        }
    }
    

    
Stop the Pipeline
    public void stopPipeline(){
        if (!){
            return;
        }
         = false;
        .shutdown();
    }
    
    // ---------------------------------------------------- Queue ------------//
  
    
    
Add an object to this pipeline
    public void addTask(Task task){
        if (.getQueue().size() >  ){
            task.cancelTask("Maximum Connections Reached: " 
                            + .getQueueSizeInBytes()
                            + " -- Retry later".);
            task.getSelectorThread().returnTask(task);
            return;                                               
        }       
        .execute((Runnable)task);
                
        if (  != null) {
            .gather(size());
        }        
    }


    
Return a Task object available in the pipeline.
    public Task getTask() {
        return null;
    }
    
    
    
Invoked when the SelectorThread is about to expire a SelectionKey.

Returns:
true if the SelectorThread should expire the SelectionKey, false if not.
    public boolean expireKey(SelectionKey key){
       return true
    }   
    
    
    
Returns the number of tasks in this Pipeline.

Returns:
Number of tasks in this Pipeline.
    public int size() {
        return .getQueue().size();
    }

    
    
Interrupt the Thread using it thread id
    public boolean interruptThread(long threadID){
            .interruptThread(threadID);
    }  
    // --------------------------------------------------Properties ----------//

    
Return the number of waiting threads.
    public int getWaitingThread(){
        return .getPoolSize() - .getActiveCount();
    }
    
    
    
Set the number of threads used by this pipeline.
    public void setMaxThreads(int maxThreads){
        this. = maxThreads;
    }    
    
    
    
Return the number of threads used by this pipeline.
    public int getMaxThreads(){
        return ;
    }
    
    
    
Return the current number of threads used.
    public int getCurrentThreadCount() {
        return .getPoolSize() ;
    }
      
      
    
Return the curent number of threads that are currently processing a task.
    public int getCurrentThreadsBusy(){
        return .getActiveCount();
    }
    
    
    
Return the maximum spare thread.
    public int getMaxSpareThreads() {
        return getWaitingThread();
    }
    
    
    
Set the thread priority of the Pipeline
    public void setPriority(int priority){
        this. = priority;
    }
    
    
    
Set the name of this Pipeline
    public void setName(String name){
        this. = name;
    }
    
    
    
Return the name of this Pipeline

Returns:
the name of this Pipeline
    public String getName(){
        return +;
    }    

    
    
Set the port used by this Pipeline

Parameters:
port the port used by this Pipeline
    public void setPort(int port){
        this. = port;
    }
    
    
    
Set the minimum thread this Pipeline will creates when initializing.

Parameters:
minThreads the minimum number of threads.
    public void setMinThreads(int minThreads){
        this. = minThreads;
    }
    
    
     
Set the maximum pending connection this Pipeline can handle.
    public void setQueueSizeInBytes(int maxQueueSizeInBytes){
        this. = maxQueueSizeInBytes;
        if (  != null )
            .setQueueSizeInBytes(maxQueueSizeInBytes);
    }
    
    
    
Get the maximum pending connection this Pipeline can handle.
    public int getQueueSizeInBytes(){
        return ;
    }  
    
    
    @Override
    public String toString(){
       return "name: " +  + " maxThreads: " +  
                + " minThreads:" + ;        
    }


    
When the maxQueueSizeInBytesConnection is reached, terminate Task
    public void rejectedExecution(Runnable rThreadPoolExecutor executor){
        Task task = (Task)r;
        task.cancelTask("Maximum Connections Reached -- Retry later"
                        .);
        task.getSelectorThread().returnTask(task);
    }
    
    
    public void setThreadsIncrement(int threadsIncrement){
        ; // Not Supported
    }
    
    
    public void setThreadsTimeout(int threadsTimeout){
        ; // Not Supported
    }


     
Return the minimum spare thread.
    public int getMinSpareThreads() {
        return 0;
    }


    
Set the minimum space thread this Pipeline can handle.
    public void setMinSpareThreads(int minSpareThreads) {
    }
    
    
    
Set the PipelineStatistic object used to gather statistic;
    public void setPipelineStatistic(PipelineStatistic pipelineStatistic){
        this. = pipelineStatistic;
    }
    
    
    
Return the PipelineStatistic object used to gather statistic;
        return ;
    }
New to GrepCode? Check out our FAQ X