Author: doogie
Date: Wed Aug 13 19:38:48 2014 New Revision: 1617816 URL: http://svn.apache.org/r1617816 Log: Now that the thread-pool startup dead-locks are fixed, we can configure the entirety of the delegator in a thread pool. This will become a big win very shortly, when multiple delegators are created at startup. Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/DelegatorFactory.java Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/DelegatorFactory.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/DelegatorFactory.java?rev=1617816&r1=1617815&r2=1617816&view=diff ============================================================================== --- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/DelegatorFactory.java (original) +++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/DelegatorFactory.java Wed Aug 13 19:38:48 2014 @@ -18,7 +18,14 @@ */ package org.ofbiz.entity; +import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; +import java.util.concurrent.FutureTask; +import java.util.concurrent.ScheduledExecutorService; + +import org.ofbiz.base.concurrent.ExecutionPool; import org.ofbiz.base.lang.Factory; import org.ofbiz.base.util.Debug; @@ -27,33 +34,65 @@ import org.ofbiz.base.util.UtilObject; /** <code>Delegator</code> factory abstract class. */ public abstract class DelegatorFactory implements Factory<Delegator, String> { public static final String module = DelegatorFactoryImpl.class.getName(); - private static final ConcurrentHashMap<String, Delegator> delegatorCache = new ConcurrentHashMap<String, Delegator>(); + private static final ConcurrentHashMap<String, Future<Delegator>> delegators = new ConcurrentHashMap<String, Future<Delegator>>(); + private static final ThreadGroup DELEGATOR_THREAD_GROUP = new ThreadGroup("DelegatorFactory"); + private static final ScheduledExecutorService executor = ExecutionPool.getExecutor(DELEGATOR_THREAD_GROUP, "delegator-startup", -1, true); public static Delegator getDelegator(String delegatorName) { + Future<Delegator> future = getDelegatorFuture(delegatorName); + try { + return future.get(); + } catch (ExecutionException e) { + Debug.logError(e, module); + return null; + } catch (InterruptedException e) { + Debug.logError(e, module); + return null; + } + } + + public static Future<Delegator> getDelegatorFuture(String delegatorName) { if (delegatorName == null) { delegatorName = "default"; //Debug.logWarning(new Exception("Location where getting delegator with null name"), "Got a getGenericDelegator call with a null delegatorName, assuming default for the name.", module); } do { - Delegator delegator = delegatorCache.get(delegatorName); + Future<Delegator> future = delegators.get(delegatorName); + if (future != null) { + //Debug.logInfo("got delegator(future(" + delegatorName + ")) from cache", module); + return future; + } + FutureTask<Delegator> futureTask = new FutureTask<Delegator>(new DelegatorConfigurable(delegatorName)); + //Debug.logInfo("putting delegator(future(" + delegatorName + ")) into cache", module); + if (delegators.putIfAbsent(delegatorName, futureTask) != null) { + continue; + } + executor.submit(futureTask); + } while (true); + } + + public static final class DelegatorConfigurable implements Callable<Delegator> { + private final String delegatorName; + + public DelegatorConfigurable(String delegatorName) { + this.delegatorName = delegatorName; + } + + public Delegator call() throws ClassNotFoundException { + try { + Delegator delegator = UtilObject.getObjectFromFactory(DelegatorFactory.class, delegatorName); - if (delegator != null) { // setup the Entity ECA Handler delegator.initEntityEcaHandler(); - //Debug.logInfo("got delegator(" + delegatorName + ") from cache", module); - + // setup the distributed CacheClear delegator.initDistributedCacheClear(); return delegator; - } - try { - delegator = UtilObject.getObjectFromFactory(DelegatorFactory.class, delegatorName); } catch (ClassNotFoundException e) { Debug.logError(e, module); + throw e; } - //Debug.logInfo("putting delegator(" + delegatorName + ") into cache", module); - delegatorCache.putIfAbsent(delegatorName, delegator); - } while (true); + } } } |
Free forum by Nabble | Edit this page |