Logo Search packages:      
Sourcecode: nailgun version File versions  Download package

NGSessionPool.java

/*   

  Copyright 2004, Martian Software, Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

*/

package com.martiansoftware.nailgun;

/**
 * Provides NGSession pooling functionality.  One parameter, "maxIdle",
 * governs its behavior by setting the maximum number of idle NGSession
 * threads it will allow.  It creates a pool of size maxIdle - 1, because
 * one NGSession is kept "on deck" by the NGServer in order to eke out
 * a little extra responsiveness.
 * 
 * @author <a href="http://www.martiansoftware.com/contact.html">Marty Lamb</a>
 */
00030 class NGSessionPool {

      /**
       * number of sessions to store in the pool
       */
00035       int poolSize = 0;

      /**
       * the pool itself
       */
00040       NGSession[] pool = null;
      
      /**
       * The number of sessions currently in the pool
       */
00045       int poolEntries = 0;

      /**
       * reference to server we're working for
       */
00050       NGServer server = null;
      
      /**
       * have we been shut down?
       */
00055       boolean done = false;
      
      /**
       * synchronization object
       */
00060       private Object lock = new Object();
      
      /**
       * Creates a new NGSessionRunner operating for the specified server, with
       * the specified number of threads
       * @param server the server to work for
       * @param poolSize the maximum number of idle threads to allow
       */
00068       NGSessionPool(NGServer server, int maxIdle) {
            this.server = server;
            this.poolSize = maxIdle - 1;
      
            pool = new NGSession[poolSize];
            poolEntries = 0;
      }

      /**
       * Returns an NGSession from the pool, or creates one if necessary
       * @return an NGSession ready to work
       */
00080       NGSession take() {
            NGSession result;
            synchronized(lock) {
                  if (poolEntries == 0) {
                        result = new NGSession(this, server);
                        result.start();
                  } else {
                        --poolEntries;
                        result = pool[poolEntries];
                  }
            }
            return (result);
      }
      
      /**
       * Returns an NGSession to the pool.  The pool may choose to shutdown
       * the thread if the pool is full
       * @param session the NGSession to return to the pool
       */
00099       void give(NGSession session) {
            if (done || poolEntries == poolSize) {
                  session.shutdown();
            } else {
                  synchronized(lock) {
                        pool[poolEntries] = session;
                        ++poolEntries;
                  }
            }
      }
      
      /**
       * Shuts down the pool.  Running nails are allowed to finish.
       */
00113       void shutdown() {
            done = true;
            synchronized(lock) {
                  while (poolEntries > 0) {
                        take().shutdown();
                  }
            }
      }

}

Generated by  Doxygen 1.6.0   Back to index