ofbiz實體引擎(一) 獲取Delegator

public abstract class DelegatorFactory implements Factory<Delegator, String> {
    public static final String module = DelegatorFactoryImpl.class.getName();
    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.getScheduledExecutor(DELEGATOR_THREAD_GROUP, "delegator-startup", Runtime.getRuntime().availableProcessors(), 10, true);

    /**
     *@author 鄭小康
     *
     * 根據delegatorName調用getDelegatorFuture方法,獲取當前delegator的 Future<Delegator>
     *
     * 而後調用get方法獲取Delegator實例
     *
     * */
    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;
        }
    }

    /**
     * @author 鄭小康
     *
     * 根據delegatorName獲取Future<Delegator> 如果爲空,新創建一個FutureTask<Delegator>將其加入到緩存中去
     *
     * 將這個futureTask給提交到線程池,futureTask中存放的是DelegatorConfigurable實例對象
     *
     *
     * */
    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 {
            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;
        }

        /**
         * 獲取delegator的具體方法
         * 並做了分佈式緩存和ECA Handler FIXME:未研究
         * */
        public Delegator call() throws ClassNotFoundException {
            try {
                Delegator delegator = UtilObject.getObjectFromFactory(DelegatorFactory.class, delegatorName);

                // setup the Entity ECA Handler
                delegator.initEntityEcaHandler();

                // setup the distributed CacheClear
                delegator.initDistributedCacheClear();

                return delegator;
            } catch (ClassNotFoundException e) {
                Debug.logError(e, module);
                throw e;
            }
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章