All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.grizzly.pool.DynamicPoolConfig Maven / Gradle / Ivy

package com.sun.grizzly.pool;

/**
 * I am a final class that provides configuration information to a DynamicPool to control its resizing behavior
 */
public final class DynamicPoolConfig {
    private static final int numberOfObjects_def = 1;
    private static final int maxGeneratingObjects_def = 1;
    //private static final int hardMaxActiveObjects_def = (int) (Runtime.getRuntime().availableProcessors() * 1.5);
    private static final int hardMaxActiveObjects_def = 2; // very much less aggresive default
    private static final int hardMinActiveObjects_def = 1;

    private static final int upThreashold_def = 10;
    private static final int downThreashold_def = 10;
    private static final int queueThreashold_def = 10;
    private static final int newThreashold_def = 10;

    private static final boolean dynamic_def = true;
    private static final boolean asyncEnabled_def = true;
    private static final boolean validate_def = false;

    private int numberOfObjects;
    private int maxGeneratingObjects;
    private int hardMaxActiveObjects;
    private int hardMinActiveObjects;

    private int upThreashold;
    private int downThreashold;
    private int queueThreashold;
    private int newThreashold;

    private boolean dynamic;
    private boolean asyncEnabled;
    private boolean validate;

    /**
     * Create a new configuration object with all default values loaded
     */

    public DynamicPoolConfig() {
        numberOfObjects = numberOfObjects_def;
        maxGeneratingObjects = maxGeneratingObjects_def;
        hardMaxActiveObjects = hardMaxActiveObjects_def;
        hardMinActiveObjects = hardMinActiveObjects_def;

        upThreashold = upThreashold_def;
        downThreashold = downThreashold_def;
        queueThreashold = queueThreashold_def;
        newThreashold = newThreashold_def;

        dynamic = dynamic_def;
        asyncEnabled = asyncEnabled_def;
        validate = validate_def;
    }

    /**
     * My my, that's a lot of parameters, isn't it? This constructor gives a large degree of control over the internal
     * logic of the dynamic pool. All parameters other than the booleans will prevent unreasonable values, such as objects < 0
     * If you want to use the default, you may deliberately enter a value of less than 0.
     *
     * @param startingNumber How many objects should the pool start with?
     * @param maxGenerating How many objects should the pool be willing to generate at a time?
     *          Note that if maxGenerating objects are being created, no requests for new objects will be considered
     * @param hardMax What is the absolute maximum number of objects that we should have in the pool?
     * @param hardMin What is the absolute minimum number of objects that we should have in the pool?
     *          The Dynamic pool will not vary outside of these limits under any circumstances
     * @param upThresh How eager should we be to increase or decrease the number of objects that we think we should have active?
     *          Higher numbers will make the pool more hesitant to change the number of active objects
     * @param downThresh How eager should we be to discard objects over the queue size?
     *          Higher numbers will make the pool more hesitant to discard objects, even if the queue is too large
     * @param queueThresh How eager should we be to increase and decrease the maximum number of idle objects?
     *          Higher numbers will make the pool more hesitant to change the number of objects that are allowed to sit idle in the queue
     * @param newThresh How eager should we be to create new objects when the pool is empty?
     *          Higher numbers will make the pool more hesitant to create new objects
     * @param async Should asynchronous (multithreaded) operation be enabled?
     *          This should be true if you want the pool to be able to service more than one request at a time
     * @param validation Should the pool attempt to validate objects on their return to the pool?
     */

    public DynamicPoolConfig (int startingNumber, int maxGenerating, int hardMax, int hardMin,
                              int upThresh, int downThresh, int queueThresh, int newThresh,
                              boolean async, boolean validation) {
        if (startingNumber > 0) {
            numberOfObjects = startingNumber;
        } else {
            numberOfObjects = numberOfObjects_def;
        }
        if (maxGenerating > 0) {
            maxGeneratingObjects = maxGenerating;
        } else {
            maxGeneratingObjects = maxGeneratingObjects_def;
        }
        if (hardMax > 0 && hardMax >= numberOfObjects) {
            hardMaxActiveObjects = hardMax;
        } else {
            hardMaxActiveObjects = Math.max(hardMaxActiveObjects_def, numberOfObjects);
        }
        if (hardMin > 0 && hardMin <= numberOfObjects) {
            if (hardMin > numberOfObjects) {
                hardMinActiveObjects = numberOfObjects;
            } else {
                hardMinActiveObjects = hardMin;
            }
        } else {
            hardMinActiveObjects = hardMinActiveObjects_def;
        }
        if (upThresh > 0) {
            upThreashold = upThresh;
        } else {
            upThreashold = upThreashold_def;
        }
        if (downThresh > 0) {
            downThreashold = downThresh;
        } else {
            downThreashold = downThreashold_def;
        }
        if (queueThresh > 0) {
            queueThreashold = queueThresh;
        } else {
            queueThreashold = queueThreashold_def;
        }
        if (newThresh > 0) {
            newThreashold = newThresh;
        } else {
            newThreashold = newThreashold_def;
        }

        asyncEnabled = async;
        validate = validation;
        String dynamicEnable = System.getProperty("jruby.runtime.dynamic");
        dynamic = dynamicEnable == null || Boolean.valueOf(dynamicEnable);
        if (hardMaxActiveObjects == numberOfObjects && numberOfObjects == hardMinActiveObjects) {
            // If someone has started us as 1/1/1 or something like that, turn off dynamic, since that will improve performance
            dynamic = false;
        }

    }

    public boolean shouldValidate() {
        return validate;
    }

    public void setValidate(boolean validate) {
        this.validate = validate;
    }

    public int getNumberOfObjects() {
        return numberOfObjects;
    }

    public void setNumberOfObjects(int numberOfObjects) {
        if (numberOfObjects > 0) {
            this.numberOfObjects = numberOfObjects;
        } else {
            this.numberOfObjects = numberOfObjects_def;
        }
    }

    public int getMaxGeneratingObjects() {
        return maxGeneratingObjects;
    }

    public void setMaxGeneratingObjects(int maxGeneratingObjects) {
        if (maxGeneratingObjects > 0) {
            this.maxGeneratingObjects = maxGeneratingObjects;
        } else {
            this.maxGeneratingObjects = maxGeneratingObjects_def;
        }
    }

    public int getHardMaxActiveObjects() {
        return hardMaxActiveObjects;
    }

    public void setHardMaxActiveObjects(int hardMaxActiveObjects) {
        if (hardMaxActiveObjects > 0) {
            this.hardMaxActiveObjects = hardMaxActiveObjects;
        } else {
            this.hardMaxActiveObjects = hardMaxActiveObjects_def;
        }
        if (this.hardMaxActiveObjects < hardMinActiveObjects) {
            hardMinActiveObjects = this.hardMaxActiveObjects;
        }
    }

    public int getHardMinActiveObjects() {
        return hardMinActiveObjects;
    }

    public void setHardMinActiveObjects(int hardMinActiveObjects) {
        if (hardMinActiveObjects > 0 && hardMinActiveObjects <= hardMaxActiveObjects) {
            this.hardMinActiveObjects = hardMinActiveObjects;
        } else {
            this.hardMinActiveObjects = Math.min(hardMinActiveObjects_def, hardMaxActiveObjects);
        }
    }

    public int getUpThreashold() {
        return upThreashold;
    }

    public void setUpThreashold(int upThreashold) {
        if (upThreashold > 0) {
            this.upThreashold = upThreashold;
        } else {
            this.upThreashold = upThreashold_def;
        }
    }

    public int getDownThreashold() {
        return downThreashold;
    }

    public void setDownThreashold(int downThreashold) {
        if (downThreashold > 0) {
            this.downThreashold = downThreashold;
        } else {
            this.downThreashold = downThreashold_def;
        }
    }

    public int getQueueThreashold() {
        return queueThreashold;
    }

    public void setQueueThreashold(int queueThreashold) {
        if (queueThreashold > 0) {
            this.queueThreashold = queueThreashold;
        } else {
            this.queueThreashold = queueThreashold_def;
        }
    }

    public int getNewThreashold() {
        return newThreashold;
    }

    public void setNewThreashold(int newThreashold) {
        if (newThreashold > 0) {
            this.newThreashold = newThreashold;
        } else {
            this.newThreashold = newThreashold_def;
        }
    }

    public boolean isDynamic() {
        return dynamic;
    }

    public boolean isAsyncEnabled() {
        return asyncEnabled;
    }

    public void setAsyncEnabled(boolean asyncEnabled) {
        this.asyncEnabled = asyncEnabled;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy