|
Glassfish example source code file (Resizer.java)
The Glassfish Resizer.java source code/* * 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.resource.pool.resizer; import org.glassfish.resource.common.PoolInfo; import com.sun.enterprise.resource.ResourceHandle; import com.sun.enterprise.resource.ResourceState; import com.sun.enterprise.resource.pool.PoolProperties; import com.sun.appserv.connectors.internal.api.PoolingException; import com.sun.enterprise.resource.allocator.ResourceAllocator; import com.sun.enterprise.resource.pool.ResourceHandler; import com.sun.enterprise.resource.pool.datastructure.DataStructure; import com.sun.logging.LogDomains; import javax.resource.ResourceException; import javax.resource.spi.ManagedConnection; import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; /** * Resizer to remove unusable connections, maintain steady-pool <br> * <code> * Remove all invalid and idle resources, as a result one of the following may happen<br> * i) equivalent to "pool-resize" quantity of resources are removed<br> * ii) less than "pool-reize" quantity of resources are removed<br> * remove more resources to match pool-resize quantity, atmost scale-down till steady-pool-size<br> * iii) more than "pool-resize" quantity of resources are removed<br> * (1) if pool-size is less than steady-pool-size, bring it back to steady-pool-size.<br> * (2) if pool-size is greater than steady-pool-size, don't do anything.<br> * * @author Jagadish Ramu */ public class Resizer extends TimerTask { protected PoolInfo poolInfo; protected DataStructure ds; protected PoolProperties pool; protected ResourceHandler handler; protected boolean preferValidateOverRecreate = false; protected final static Logger _logger = LogDomains.getLogger(Resizer.class, LogDomains.RSR_LOGGER); public Resizer(PoolInfo poolInfo, DataStructure ds, PoolProperties pp, ResourceHandler handler, boolean preferValidateOverRecreate) { this.poolInfo = poolInfo; this.ds = ds; this.pool = pp; this.handler = handler; this.preferValidateOverRecreate = preferValidateOverRecreate; } public void run() { debug("Resizer for pool " + poolInfo); try { resizePool(true); } catch(Exception ex) { Object[] params = new Object[]{poolInfo, ex.getMessage()}; _logger.log(Level.WARNING, "resource_pool.resize_pool_error", params); } } /** * Resize the pool * * @param forced when force is true, scale down the pool. */ public void resizePool(boolean forced) { //If the wait queue is NOT empty, don't do anything. if (pool.getWaitQueueLength() > 0) { return; } //remove invalid and idle resource(s) int noOfResourcesRemoved = removeIdleAndInvalidResources(); int poolScaleDownQuantity = pool.getResizeQuantity() - noOfResourcesRemoved; //scale down pool by atmost "resize-quantity" scaleDownPool(poolScaleDownQuantity, forced); //ensure that steady-pool-size is maintained ensureSteadyPool(); debug("No. of resources held for pool [ " + poolInfo + " ] : " + ds.getResourcesSize()); } /** * Make sure that steady pool size is maintained after all idle-timed-out, * invalid and scale-down resource removals. */ private void ensureSteadyPool() { if (ds.getResourcesSize() < pool.getSteadyPoolSize()) { // Create resources to match the steady pool size for (int i = ds.getResourcesSize(); i < pool.getSteadyPoolSize(); i++) { try { handler.createResourceAndAddToPool(); } catch (PoolingException ex) { Object[] params = new Object[]{poolInfo, ex.getMessage()}; _logger.log(Level.WARNING, "resource_pool.resize_pool_error", params); } } } } /** * Scale down pool by a <code>size <= pool-resize-quantity * * @param forced scale-down only when forced * @param scaleDownQuantity no. of resources to remove */ protected void scaleDownPool(int scaleDownQuantity, boolean forced) { if (pool.getResizeQuantity() > 0 && forced) { scaleDownQuantity = (scaleDownQuantity <= (ds.getResourcesSize() - pool.getSteadyPoolSize())) ? scaleDownQuantity : 0; ResourceHandle h; while (scaleDownQuantity > 0 && ((h = ds.getResource()) != null)) { ds.removeResource(h); scaleDownQuantity--; } } } /** * Get the free connections list from the pool, remove idle-timed-out resources * and then invalid resources. * * @return int number of resources removed */ protected int removeIdleAndInvalidResources() { int poolSizeBeforeRemoval = ds.getResourcesSize(); int noOfResourcesRemoved; //Find all Connections that are free/not-in-use ResourceState state; int size = ds.getFreeListSize(); // let's cache the current time since precision is not required here. long currentTime = System.currentTimeMillis(); int validConnectionsCounter = 0; int idleConnKeptInSteadyCounter = 0; //iterate through all thre active resources to find idle-time lapsed ones. ResourceHandle h; Set<ResourceHandle> activeResources = new HashSet Other Glassfish examples (source code examples)Here is a short list of links related to this Glassfish Resizer.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.