Package org.jvnet.glassfish.comms.clb.proxy.outbound.connectioncache

Source Code of org.jvnet.glassfish.comms.clb.proxy.outbound.connectioncache.HttpClbConnections

/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
* Copyright (c) Ericsson AB, 2004-2008. 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.html
* or glassfish/bootstrap/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 glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code.  If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [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 org.jvnet.glassfish.comms.clb.proxy.outbound.connectioncache;

import com.sun.grizzly.ConnectorHandler;
import java.util.HashMap;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import org.jvnet.glassfish.comms.clb.proxy.api.Endpoint;

/**
*
* @author
*/
public class HttpClbConnections  {
   
    private final BlockingQueue<ConnectorHandler> idleConnections =
            new LinkedBlockingQueue<ConnectorHandler>();
   
    private Endpoint endpoint = null;
   
    private int maxConnections = 10;
   
    private long starttime = 0;
   
    private static long WAIT_TIME = 50;
   
    // default is 5 less than the number in domain.xml
    private long maxKeepAliveRequestsPerConnection = 245;
   
    private KeepAliveCounter keepAliveCounter = null;
   
    // default is 10% less than default in domain.xml
    private long keepAliveIdleTimeOut = 27000;
       
    HttpClbConnections(Endpoint ep, int maxconnections) {
        endpoint = ep;
        maxConnections = maxconnections;
        starttime = System.currentTimeMillis();
        keepAliveCounter = new KeepAliveCounter();       
    }

    public void setMaxRequestsPerConnection(long requests){
        maxKeepAliveRequestsPerConnection = requests - 10;
    }  
   
    public void setKeepAliveIdleTimeout(long time){
        keepAliveIdleTimeOut = (long) (0.9 * time);
    }
   
   
    public ConnectorHandler getConnection()  {  
        ConnectorHandler connection  = null;
        try {           
            connection = idleConnections.poll(WAIT_TIME,
                    TimeUnit.MILLISECONDS);
            if (connection != null){
                if (shouldCheckKeepAlive() &&
                        !keepAliveCounter.isUsable(connection)){
                    try {
                        connection.close();
                    } catch (Exception e){
                        ;
                    }
                    removeConnection(connection);
                    connection = null;
                }
            }           
        } catch (InterruptedException ex) {
            ;
        }
        return connection;
    }

    public boolean putConnection(ConnectorHandler connection) {
        // make it simple  for now       
        if (idleConnections.size() < maxConnections) {
            if (shouldCheckKeepAlive() &&
                    !keepAliveCounter.canRelease(connection)) {
                return false;
            }
            idleConnections.offer(connection);
            return true;
        }
        return false;
    }
   
    private boolean shouldCheckKeepAlive(){
       return ((maxKeepAliveRequestsPerConnection > 0) ||
                    (keepAliveIdleTimeOut > 0));
    }
   
    public void removeConnection(ConnectorHandler connection) {  
        if (connection != null){
            idleConnections.remove(connection);
            keepAliveCounter.removeStats(connection);           
        }
        //print();
    }
   
    private void print(){
        long currenttime = System.currentTimeMillis();
        if ((currenttime - starttime) > 5000){         
            starttime = currenttime;
        }
    }
   
   
    private class KeepAliveCounter {

        HashMap<ConnectorHandler, KeepAliveStats> keepAliveCounterMap =
                new HashMap<ConnectorHandler, KeepAliveStats>();     

      
        public void removeStats(ConnectorHandler connection){
            keepAliveCounterMap.remove(connection);
        }
       
        private KeepAliveStats getStatistics(ConnectorHandler connection){
            KeepAliveStats stats = keepAliveCounterMap.get(connection);
            if (stats == null) {
                stats = new KeepAliveStats(1);
                stats.updateLastAccessTime();
                keepAliveCounterMap.put(connection, stats);
            }
            return stats;
        }
       
        public boolean canRelease(ConnectorHandler connection){           
            KeepAliveStats stats = getStatistics(connection);
            if (keepAliveIdleTimeOut > 0){
                stats.updateLastAccessTime();               
            }
            if ((maxKeepAliveRequestsPerConnection < 0) ||
                    (stats.getCount() < maxKeepAliveRequestsPerConnection)){               
                return true;
            }
            return false;
        }
       
        public boolean isUsable(ConnectorHandler connection) {           
            KeepAliveStats stats = getStatistics(connection);
            if (maxKeepAliveRequestsPerConnection > 0){
                stats.incrementCounter();               
            }
            long currentTime = System.currentTimeMillis();
            if ((keepAliveIdleTimeOut < 0) ||
                    (currentTime - stats.getLastAccessTime()) < keepAliveIdleTimeOut) {
                return true;
            }
            return false;
        }
    }
   
    private class KeepAliveStats {
       
        long counter = 0;
       
        long lastAccessTime = -1;

        private KeepAliveStats(long i) {
            counter = i;
        }
       
        public void incrementCounter() {
            counter++;           
        }
       
        public void updateLastAccessTime() {
            lastAccessTime = System.currentTimeMillis();
        }
       
        public long getLastAccessTime(){
            return lastAccessTime;
        }
       
        public long getCount(){
            return counter;
        }
    }
}
TOP

Related Classes of org.jvnet.glassfish.comms.clb.proxy.outbound.connectioncache.HttpClbConnections

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.