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;
            }
        }
    }
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏日常分享

Struts2+DAO层实现实例02——搭建DAO基本框架并与Struts2组合

985
来自专栏用户画像

struts2 文件上传

793
来自专栏Java帮帮-微信公众号-技术文章全总结

Java面试系列17-编程题-读取服务器字符、实现序列化、计数器、1000阶乘、n出列问题等

一,Java的通信编程,编程题(或问答),用JAVA SOCKET编程,读服务器几个字符,再写入本地显示? Server端程序: package test;...

3798
来自专栏Hongten

模仿Hibernate的逆向工程_java版_源码下载

在这篇blog:"Hibernate逆向工程原理_java版本"中谈到了Hibernate逆向工程原理。

974
来自专栏码匠的流水账

聊聊rocketmq的NettyClientConfig

org/apache/rocketmq/remoting/netty/NettyClientConfig.java

511
来自专栏Java帮帮-微信公众号-技术文章全总结

Java基础-21(02)总结字符流,IO流编码问题,实用案例必做一遍

C:把集合中的数据存储到文本文件 package cn.itcast_02; import java.io.BufferedWriter; import jav...

3584
来自专栏码匠的流水账

聊聊storm worker的executor与task

storm-2.0.0/storm-client/src/jvm/org/apache/storm/daemon/worker/Worker.java

662
来自专栏码匠的流水账

聊聊storm的AssignmentDistributionService

本文主要研究一下storm的AssignmentDistributionService

1131
来自专栏阿炬.NET

Bonobo创建新库出错,解决方案

3547
来自专栏Netkiller

Spring boot with Oracle

本文节选自《Netkiller Java 手札》 地址: http://www.netkiller.cn/java/index.html 9.11. Sprin...

2794

扫码关注云+社区