Author: doogie
Date: Sun Jun 26 02:50:49 2011 New Revision: 1139700 URL: http://svn.apache.org/viewvc?rev=1139700&view=rev Log: FEATURE: Add a thread pool to GenericHelper; this is per datasource. Nothing makes use of this yet. Modified: ofbiz/trunk/framework/entity/dtd/entity-config.xsd ofbiz/trunk/framework/entity/src/org/ofbiz/entity/config/DatasourceInfo.java ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericDAO.java ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericHelper.java ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericHelperDAO.java ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/MemoryHelper.java ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/DatabaseUtil.java Modified: ofbiz/trunk/framework/entity/dtd/entity-config.xsd URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/dtd/entity-config.xsd?rev=1139700&r1=1139699&r2=1139700&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/dtd/entity-config.xsd (original) +++ ofbiz/trunk/framework/entity/dtd/entity-config.xsd Sun Jun 26 02:50:49 2011 @@ -392,6 +392,7 @@ under the License. <xs:attribute type="xs:string" name="table-type"/> <xs:attribute type="xs:string" name="character-set"/> <xs:attribute type="xs:string" name="collate"/> + <xs:attribute type="xs:integer" name="max-worker-pool-size" default="0"/> </xs:attributeGroup> <xs:element name="sql-load-path"> <xs:complexType> Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/config/DatasourceInfo.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/config/DatasourceInfo.java?rev=1139700&r1=1139699&r2=1139700&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/config/DatasourceInfo.java (original) +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/config/DatasourceInfo.java Sun Jun 26 02:50:49 2011 @@ -76,6 +76,7 @@ public class DatasourceInfo { public String tableType = null; public String characterSet = null; public String collate = null; + public int maxWorkerPoolSize = 1; public DatasourceInfo(Element element) { this.name = element.getAttribute("name"); @@ -110,6 +111,7 @@ public class DatasourceInfo { Debug.logWarning("datasource def not found with name " + this.name + ", using default for table-type (none)", module); Debug.logWarning("datasource def not found with name " + this.name + ", using default for character-set (none)", module); Debug.logWarning("datasource def not found with name " + this.name + ", using default for collate (none)", module); + Debug.logWarning("datasource def not found with name " + this.name + ", using default for max-worker-pool-size (1)", module); } else { this.schemaName = datasourceElement.getAttribute("schema-name"); // anything but false is true @@ -161,6 +163,16 @@ public class DatasourceInfo { this.tableType = datasourceElement.getAttribute("table-type"); this.characterSet = datasourceElement.getAttribute("character-set"); this.collate = datasourceElement.getAttribute("collate"); + try { + this.maxWorkerPoolSize = Integer.parseInt(datasourceElement.getAttribute("max-worker-pool-size")); + if (this.maxWorkerPoolSize == 0) { + this.maxWorkerPoolSize = 1; + } else if (this.maxWorkerPoolSize < -2) { + this.maxWorkerPoolSize = -1; + } + } catch (Exception e) { + Debug.logWarning("Could not parse result-fetch-size value for datasource with name " + this.name + ", using JDBC driver default value", module); + } } if (UtilValidate.isEmpty(this.fkStyle)) this.fkStyle = "name_constraint"; if (UtilValidate.isEmpty(this.joinStyle)) this.joinStyle = "ansi"; Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericDAO.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericDAO.java?rev=1139700&r1=1139699&r2=1139700&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericDAO.java (original) +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericDAO.java Sun Jun 26 02:50:49 2011 @@ -29,11 +29,15 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeSet; +import java.util.concurrent.Callable; +import java.util.concurrent.Future; +import java.util.concurrent.ExecutorService; import javolution.util.FastList; import javolution.util.FastMap; import javolution.util.FastSet; +import org.ofbiz.base.concurrent.ExecutionPool; import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.UtilValidate; import org.ofbiz.entity.Delegator; @@ -72,9 +76,11 @@ public class GenericDAO { public static final String module = GenericDAO.class.getName(); private static final FastMap<String, GenericDAO> genericDAOs = FastMap.newInstance(); + private static final ThreadGroup GENERIC_DAO_THREAD_GROUP = new ThreadGroup("GenericDAO"); private final GenericHelperInfo helperInfo; private final ModelFieldTypeReader modelFieldTypeReader; private final DatasourceInfo datasourceInfo; + private final ExecutorService executor; public static GenericDAO getGenericDAO(GenericHelperInfo helperInfo) { GenericDAO newGenericDAO = genericDAOs.get(helperInfo.getHelperFullName()); @@ -90,6 +96,11 @@ public class GenericDAO { this.helperInfo = helperInfo; this.modelFieldTypeReader = ModelFieldTypeReader.getModelFieldTypeReader(helperInfo.getHelperBaseName()); this.datasourceInfo = EntityConfigUtil.getDatasourceInfo(helperInfo.getHelperBaseName()); + this.executor = ExecutionPool.getExecutor(GENERIC_DAO_THREAD_GROUP, "entity-datasource(" + helperInfo.getHelperFullName() + ")", datasourceInfo.maxWorkerPoolSize, false); + } + + public <T> Future<T> submitWork(Callable<T> callable) throws GenericEntityException { + return this.executor.submit(callable); } private void addFieldIfMissing(List<ModelField> fieldsToSave, String fieldName, ModelEntity modelEntity) { @@ -1164,13 +1175,13 @@ public class GenericDAO { /* ====================================================================== */ public void checkDb(Map<String, ModelEntity> modelEntities, List<String> messages, boolean addMissing) { - DatabaseUtil dbUtil = new DatabaseUtil(this.helperInfo); + DatabaseUtil dbUtil = new DatabaseUtil(this.helperInfo, this.executor); dbUtil.checkDb(modelEntities, messages, addMissing); } /** Creates a list of ModelEntity objects based on meta data from the database */ public List<ModelEntity> induceModelFromDb(Collection<String> messages) { - DatabaseUtil dbUtil = new DatabaseUtil(this.helperInfo); + DatabaseUtil dbUtil = new DatabaseUtil(this.helperInfo, this.executor); return dbUtil.induceModelFromDb(messages); } } Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericHelper.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericHelper.java?rev=1139700&r1=1139699&r2=1139700&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericHelper.java (original) +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericHelper.java Sun Jun 26 02:50:49 2011 @@ -24,6 +24,8 @@ import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.concurrent.Callable; +import java.util.concurrent.Future; import org.ofbiz.entity.GenericEntityException; import org.ofbiz.entity.GenericPK; @@ -46,6 +48,8 @@ public interface GenericHelper { */ public String getHelperName(); + public <T> Future<T> submitWork(Callable<T> callable) throws GenericEntityException; + /** Creates a Entity in the form of a GenericValue and write it to the database *@return GenericValue instance containing the new instance */ Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericHelperDAO.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericHelperDAO.java?rev=1139700&r1=1139699&r2=1139700&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericHelperDAO.java (original) +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/GenericHelperDAO.java Sun Jun 26 02:50:49 2011 @@ -23,6 +23,8 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.concurrent.Callable; +import java.util.concurrent.Future; import org.ofbiz.base.util.Debug; import org.ofbiz.entity.GenericEntityException; @@ -54,6 +56,10 @@ public class GenericHelperDAO implements return this.helperInfo.getHelperFullName(); } + public <T> Future<T> submitWork(Callable<T> callable) throws GenericEntityException { + return genericDAO.submitWork(callable); + } + /** Creates a Entity in the form of a GenericValue and write it to the database *@return GenericValue instance containing the new instance */ Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/MemoryHelper.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/MemoryHelper.java?rev=1139700&r1=1139699&r2=1139700&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/MemoryHelper.java (original) +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/datasource/MemoryHelper.java Sun Jun 26 02:50:49 2011 @@ -27,12 +27,18 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.concurrent.Callable; +import java.util.concurrent.Future; +import java.util.concurrent.ExecutorService; +import org.ofbiz.base.concurrent.ExecutionPool; import org.ofbiz.base.util.Debug; import org.ofbiz.entity.GenericEntityException; import org.ofbiz.entity.GenericNotImplementedException; import org.ofbiz.entity.GenericPK; import org.ofbiz.entity.GenericValue; +import org.ofbiz.entity.config.DatasourceInfo; +import org.ofbiz.entity.config.EntityConfigUtil; import org.ofbiz.entity.condition.EntityCondition; import org.ofbiz.entity.jdbc.SqlJdbcUtil; import org.ofbiz.entity.model.ModelEntity; @@ -52,12 +58,14 @@ public class MemoryHelper implements Gen public static final String module = MemoryHelper.class.getName(); private static Map<String, HashMap<GenericPK, GenericValue>> cache = new HashMap<String, HashMap<GenericPK, GenericValue>>(); + private static final ThreadGroup MEMORY_HELPER_THREAD_GROUP = new ThreadGroup("MemoryHelper"); public static void clearCache() { cache = new HashMap<String, HashMap<GenericPK, GenericValue>>(); } private String helperName; + protected ExecutorService executor; private boolean addToCache(GenericValue value) { if (value == null) { @@ -272,12 +280,18 @@ public class MemoryHelper implements Gen public MemoryHelper(String helperName) { this.helperName = helperName; modelFieldTypeReader = ModelFieldTypeReader.getModelFieldTypeReader(helperName); + DatasourceInfo datasourceInfo = EntityConfigUtil.getDatasourceInfo(helperName); + this.executor = ExecutionPool.getExecutor(MEMORY_HELPER_THREAD_GROUP, "entity-datasource(" + helperName + ")", datasourceInfo.maxWorkerPoolSize, false); } public String getHelperName() { return helperName; } + public <T> Future<T> submitWork(Callable<T> callable) throws GenericEntityException { + return this.executor.submit(callable); + } + public GenericValue create(GenericValue value) throws GenericEntityException { if (addToCache(value)) { return value; Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/DatabaseUtil.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/DatabaseUtil.java?rev=1139700&r1=1139699&r2=1139700&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/DatabaseUtil.java (original) +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/jdbc/DatabaseUtil.java Sun Jun 26 02:50:49 2011 @@ -32,6 +32,10 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeSet; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Future; +import java.util.concurrent.FutureTask; import javolution.util.FastList; import javolution.util.FastMap; @@ -76,12 +80,18 @@ public class DatabaseUtil { protected String password = null; boolean isLegacy = false; + protected ExecutorService executor; // OFBiz DatabaseUtil public DatabaseUtil(GenericHelperInfo helperInfo) { + this(helperInfo, null); + } + + public DatabaseUtil(GenericHelperInfo helperInfo, ExecutorService executor) { this.helperInfo = helperInfo; this.modelFieldTypeReader = ModelFieldTypeReader.getModelFieldTypeReader(helperInfo.getHelperBaseName()); this.datasourceInfo = EntityConfigUtil.getDatasourceInfo(helperInfo.getHelperBaseName()); + this.executor = executor; } // Legacy DatabaseUtil @@ -93,6 +103,31 @@ public class DatabaseUtil { this.isLegacy = true; } + protected <T> Future<T> submitWork(Callable<T> callable) { + if (this.executor == null) { + FutureTask<T> task = new FutureTask<T>(callable); + task.run(); + return task; + } + return this.executor.submit(callable); + } + + protected <T> List<Future<T>> submitAll(Collection<? extends Callable<T>> tasks) { + List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size()); + if (this.executor == null) { + for (Callable<T> callable: tasks) { + FutureTask<T> task = new FutureTask<T>(callable); + task.run(); + futures.add(task); + } + return futures; + } + for (Callable<T> callable: tasks) { + futures.add(this.executor.submit(callable)); + } + return futures; + } + protected Connection getConnection() throws SQLException, GenericEntityException { Connection connection = null; if (!isLegacy) { |
Free forum by Nabble | Edit this page |