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

src.main.java.org.kawanfw.sql.tomcat.ThreadPoolProperties Maven / Gradle / Ivy

Go to download

AceQL HTTP is a framework of REST like http APIs that allow to access to remote SQL databases over http from any device that supports http. AceQL HTTP is provided with three client SDK: - The AceQL C# Client SDK allows to wrap the HTTP APIs using Microsoft SQL Server like calls in their code, just like they would for a local database. - The AceQL Java Client JDBC Driver allows to wrap the HTTP APIs using JDBC calls in their code, just like they would for a local database. - The AceQL Python Client SDK allows SQL calls to be encoded with standard unmodified DB-API 2.0 syntax

There is a newer version: 12.2
Show newest version
package org.kawanfw.sql.tomcat;

import java.lang.reflect.Constructor;
import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;
import org.kawanfw.sql.api.server.DatabaseConfigurationException;
import org.kawanfw.sql.util.SqlTag;

public class ThreadPoolProperties {

    public static final int DEFAULT_CORE_POOL_SIZE = 100;
    public static final int DEFAULT_MAXIMUM_POOL_SIZE = 200;
    public static final int DEFAULT_KEEP_ALIVE_TIME = 10;
    public static final int DEFAULT_BLOCKING_QUEUE_CAPACITY = 50000;

    private String corePoolSizeStr;
    private String maximumPoolSizeStr;
    private String unitStr;
    private  String keepAliveTimeStr;
    private String workQueueClassName;
    private String capacityStr;

    private int corePoolSize= DEFAULT_CORE_POOL_SIZE;
    private int maximumPoolSize= DEFAULT_MAXIMUM_POOL_SIZE;
    private TimeUnit unit =TimeUnit.SECONDS;
    private int keepAliveTime= DEFAULT_KEEP_ALIVE_TIME;
    private int capacity= DEFAULT_BLOCKING_QUEUE_CAPACITY;
    private BlockingQueue workQueue;

    public ThreadPoolProperties(Properties properties) {
	Objects.requireNonNull(properties, "properties cannot be null!");

	corePoolSizeStr = properties.getProperty("corePoolSize");
	maximumPoolSizeStr = properties.getProperty("maximumPoolSize");
	unitStr = properties.getProperty("unit");
	keepAliveTimeStr = properties.getProperty("keepAliveTime");
	workQueueClassName = properties.getProperty("workQueueClassName");
	capacityStr = properties.getProperty("capacity");

	checkAndFillParameters();
	createWorkingQueue();
    }


    @SuppressWarnings("unchecked")
    private void createWorkingQueue() {
	String className = null;
	if (workQueueClassName != null) {
	    className = workQueueClassName;
	} else {
	    className = "java.util.concurrent.ArrayBlockingQueue";
	}

	Class clazz = null;
	// Create Queue
	try {
	    clazz = Class.forName(className);

	    if (capacity > 0) {
		Constructor constructor = clazz.getConstructor(int.class);
		workQueue = (BlockingQueue) constructor.newInstance(capacity);
	    } else {
		Constructor constructor = clazz.getConstructor();
		workQueue = (BlockingQueue) constructor.newInstance();
	    }

	} catch (Exception e) {
	    String CR_LF = System.getProperty("line.separator");
	    throw new DatabaseConfigurationException("blockingQueueClassName instance for name " + className
		    + " could not be created." + CR_LF + "Reason: " + e.toString() + ". " + SqlTag.PLEASE_CORRECT);
	}
    }

    private void checkAndFillParameters() {
	if (corePoolSizeStr != null) {
	    throwExceptionValueIfNotNumeric("corePoolSize", corePoolSizeStr);
	    corePoolSize = Integer.parseInt(corePoolSizeStr);
	}

	if (maximumPoolSizeStr != null) {
	    throwExceptionValueIfNotNumeric("maximumPoolSize", maximumPoolSizeStr);
	    maximumPoolSize = Integer.parseInt(maximumPoolSizeStr);
	}

	if (unitStr != null) {
	    try {
		unit = TimeUnit.valueOf(unitStr);
	    } catch (Exception e) {
		throw new DatabaseConfigurationException(
			"unit value is invalid: " + unitStr + ". " + SqlTag.PLEASE_CORRECT);
	    }
	}

	if (keepAliveTimeStr != null) {
	    throwExceptionValueIfNotNumeric("keepAliveTime", keepAliveTimeStr);
	    keepAliveTime = Integer.parseInt(keepAliveTimeStr);
	}

	if (capacityStr != null) {
	    throwExceptionValueIfNotNumeric("capacity", capacityStr);
	    capacity= Integer.parseInt(capacityStr);
	}

	if (maximumPoolSize < corePoolSize) {
	    throw new DatabaseConfigurationException("maximumPoolSize (" + maximumPoolSize + ") is < corePoolSize ("
		    + corePoolSize + "). maximumPoolSize Must be >= corePoolSize. " + SqlTag.PLEASE_CORRECT);
	}

	if (capacity < 0) {
	    throw new DatabaseConfigurationException("capacity must be >= 0. " + SqlTag.PLEASE_CORRECT);
	}
    }

    private void throwExceptionValueIfNotNumeric(String name, String value) {
	if (!StringUtils.isNumeric(value)) {
	    throw new DatabaseConfigurationException(name + " property is not numeric. " + SqlTag.PLEASE_CORRECT);
	}
    }

    public String getCapacityStr() {
        return capacityStr;
    }

    public int getCorePoolSize() {
        return corePoolSize;
    }

    public int getMaximumPoolSize() {
        return maximumPoolSize;
    }

    public TimeUnit getUnit() {
        return unit;
    }

    public int getKeepAliveTime() {
        return keepAliveTime;
    }

    public int getCapacity() {
        return capacity;
    }

    public BlockingQueue getWorkQueue() {
        return workQueue;
    }

    public void setCapacityStr(String capacityStr) {
        this.capacityStr = capacityStr;
    }

    public void setCorePoolSize(int corePoolSize) {
        this.corePoolSize = corePoolSize;
    }

    public void setMaximumPoolSize(int maximumPoolSize) {
        this.maximumPoolSize = maximumPoolSize;
    }

    public void setUnit(TimeUnit unit) {
        this.unit = unit;
    }

    public void setKeepAliveTime(int keepAliveTime) {
        this.keepAliveTime = keepAliveTime;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    public void setWorkQueue(BlockingQueue workQueue) {
        this.workQueue = workQueue;
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy