美文网首页
hsf笔记-HSFServiceContainer

hsf笔记-HSFServiceContainer

作者: 兴浩 | 来源:发表于2018-08-12 20:41 被阅读10次

笔记基于2.2.6.1-EDAS版本,笔记只作为自我学习记录使用,均为个人理解

该框架自成体系,很大部分参考了Spring,但是并没有依赖,可以理解为阿里定制版Spring框架

1.HSFServiceContainer

服务容器的入口辅助类,贯穿系统全局,其内部是对AppServiceContainer的封装

HSFServiceContainer部分源码

public class HSFServiceContainer {
    public static final AppServiceContainer SHARED_CONTAINER = new AppServiceContainer();

    public HSFServiceContainer() {
    }

    public static <T> T getInstance(Class<T> classType) {
        AppServiceContainer appServiceContainer = getAppServiceContainer(classType);
        return appServiceContainer.getInstance(classType);
    }

    private static <T> AppServiceContainer getAppServiceContainer(Class<T> classType) {
        return AppServiceContainer.isSharedType(classType) ? SHARED_CONTAINER : ApplicationModelFactory.getCurrentApplication().getServiceContainer();
    }
}

示例代码

public class ConfigServiceTest {

    public static void main(String[] args)
    {
        ConfigService service=HSFServiceContainer.getInstance(ConfigService.class);
        Config config=service.getConfig();
    }
}

2. ApplicationModel

如果作为客户端程序的话,应该都知道每个App入口都有一个Application类,该类的作用职责应该也是如此,ApplicationModel有一个工厂类ApplicationModelFactory,实例由工厂创建,这里需要注意的是每次拿到的对象是当前线程的,再回头看HSFServiceContainer类中有一个SHARED_CONTAINER,当Service标记为Shared注解,则可以实现全局容器共享

public class ApplicationModelFactory {

    static {
        CURRENT_APPLICATION = new ThreadLocal();
    }
   
    private static ThreadLocal<ApplicationModel> CURRENT_APPLICATION;
    public static ApplicationModel getCurrentApplication() {
        if (CURRENT_APPLICATION.get() == null) {
            String stack = ExceptionUtils.getFullStackTrace(new IllegalStateException("Application not found"));
            LOGGER.info(stack);
            CURRENT_APPLICATION.set(getMainApplicationModel());
        }
        return (ApplicationModel)CURRENT_APPLICATION.get();
    }

    public static ApplicationModel getMainApplicationModel() {
        if (MAIN_APPLICATION_MODEL == null) {
            MAIN_APPLICATION_MODEL = new ApplicationModel(AppInfoUtils.getAppName(), ApplicationModelFactory.class.getClassLoader(), true);
            MAIN_APPLICATION_MODEL.init();
        }

        return MAIN_APPLICATION_MODEL;
    }
}

2.1 AppInfoUtils

AppInfoUtils辅助类用于获取当前运行环境,比如pandora、jboss、jetty、tomcat

public class AppInfoUtils {
    public static final String PARAM_MARKING_PROJECT = "project.name";
    private static final String PARAM_MARKING_JBOSS = "jboss.server.home.dir";
    private static final String PARAM_MARKING_JETTY = "jetty.home";
    private static final String PARAM_MARKING_TOMCAT = "catalina.base";
    private static final String LINUX_ADMIN_HOME = "/home/admin/";
    private static final String SERVER_JBOSS = "jboss";
    private static final String SERVER_JETTY = "jetty";
    private static final String SERVER_TOMCAT = "tomcat";
    private static final String SERVER_UNKNOWN = "unknown server";
    private static final String HOST_TYPE = "pandora.host.type";
    private static String appName = null;
    private static String serverType = null;
    public static final AtomicBoolean appRunning = new AtomicBoolean(false);
    public static int hsfSpringBeanCountDown = 0;
    public static int dubboSpringBeanCountDown = 0;
    private static boolean enableHttp = false;

    public AppInfoUtils() {
    }

    public static String getAppName() {
        return appName;
    }

    public static void initAppName() {
        if (appName == null) {
            appName = System.getProperty("project.name");
            if (appName == null) {
                String serverHome = null;
                if ("jboss".equals(getServerType())) {
                    serverHome = System.getProperty("jboss.server.home.dir");
                } else if ("jetty".equals(getServerType())) {
                    serverHome = System.getProperty("jetty.home");
                } else if ("tomcat".equals(getServerType())) {
                    serverHome = System.getProperty("catalina.base");
                }

                if (serverHome != null && serverHome.startsWith("/home/admin/")) {
                    appName = StringUtils.substringBetween(serverHome, "/home/admin/", File.separator);
                }

                if (appName == null) {
                    if (appName == null) {
                        appName = "unknown";
                    }

                }
            }
        }
    }

    public static String getHSFLogPath() {
        return System.getProperty("HSF.LOG.PATH");
    }

    public static String getServerType() {
        if (serverType != null) {
            return serverType;
        } else {
            if (System.getProperty("pandora.host.type") != null) {
                serverType = System.getProperty("pandora.host.type");
            } else if (System.getProperty("jboss.server.home.dir") != null) {
                serverType = "jboss";
            } else if (System.getProperty("jetty.home") != null) {
                serverType = "jetty";
            } else if (System.getProperty("catalina.base") != null) {
                serverType = "tomcat";
            } else {
                serverType = "unknown server";
            }

            return serverType;
        }
    }
    static {
        initAppName();
    }
}

3. AppServiceContainer的创建

由于ApplicationModel中包含了AppServiceContainer对象,并在构造函数中创建,所以获取到ApplicationModel实例并调用getServiceContainer方法就可以拿到AppServiceContainer实例

public class ApplicationModel extends ContainerBase {
    private AppServiceContainer serviceContainer;
    public ApplicationModel(String name, ClassLoader appContextClassLoader, boolean isBiz) {
        super(frameworkModel);
        this.serviceContainer = HSFServiceContainer.createAppServiceContainer(this);
    }

    public AppServiceContainer getServiceContainer() {
        return this.serviceContainer;
    }

总结:

  1. HSFServiceContainer是获取Service的快捷入口
  2. AppServiceContainer是一个Ioc容器,类似Spring的BeanFactory
  3. ApplicationModel是程序系统类,并与AppServiceContainer关联.需要注意其是线程本地共享实例

相关文章

网友评论

      本文标题:hsf笔记-HSFServiceContainer

      本文链接:https://www.haomeiwen.com/subject/hmhebftx.html