聊聊OtterLauncher

﹥>﹥吖頭↗ 提交于 2020-10-15 00:42:37

本文主要研究一下OtterLauncher

OtterLauncher

otter/node/deployer/src/main/java/com/alibaba/otter/node/deployer/OtterLauncher.java

public class OtterLauncher {

    private static final Logger logger = LoggerFactory.getLogger(OtterLauncher.class);

    public static void main(String[] args) throws Throwable {
        // 启动dragoon client
        // startDragoon();
        // logger.info("INFO ## the dragoon is start now ......");
        final OtterController controller = OtterContextLocator.getOtterController();
        controller.start();
        try {
            logger.info("INFO ## the otter server is running now ......");
            Runtime.getRuntime().addShutdownHook(new Thread() {

                public void run() {
                    try {
                        logger.info("INFO ## stop the otter server");
                        controller.stop();
                    } catch (Throwable e) {
                        logger.warn("WARN ##something goes wrong when stopping Otter Server:\n{}",
                            ExceptionUtils.getFullStackTrace(e));
                    } finally {
                        logger.info("INFO ## otter server is down.");
                    }
                }

            });
        } catch (Throwable e) {
            logger.error("ERROR ## Something goes wrong when starting up the Otter Server:\n{}",
                ExceptionUtils.getFullStackTrace(e));
            System.exit(0);
        }
    }

    // 启动dragoon client
    // private static void startDragoon() {
    // do nothing
    // }
}
  • OtterLauncher的main方法通过OtterContextLocator.getOtterController()获取OtterController,然后执行其start方法,之后注册shutdownHook去执行其stop方法

OtterContextLocator

otter/node/etl/src/main/java/com/alibaba/otter/node/etl/OtterContextLocator.java

public class OtterContextLocator {

    private static ClassPathXmlApplicationContext context       = null;
    private static RuntimeException               initException = null;

    static {
        try {
            context = new ClassPathXmlApplicationContext("applicationContext.xml") {

                @Override
                protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
                    super.customizeBeanFactory(beanFactory);
                    beanFactory.setAllowBeanDefinitionOverriding(false);
                }
            };
        } catch (RuntimeException e) {
            throw new ConfigException("ERROR ## ", e);
        }
    }

    private static ApplicationContext getApplicationContext() {
        if (context == null) {
            throw initException;
        }

        return context;
    }

    public static void close() {
        ((ClassPathXmlApplicationContext) context).close();
    }

    public static OtterController getOtterController() {
        return (OtterController) getApplicationContext().getBean("otterController");
    }

    public static <T> T getBean(String name) {
        return (T) getApplicationContext().getBean(name);
    }

    /**
     * 根据当前spring容器的bean定义,解析对应的object并完成注入
     */
    public static void autowire(Object obj) {
        // 重新注入一下对象
        context.getAutowireCapableBeanFactory().autowireBeanProperties(obj,
                                                                       AutowireCapableBeanFactory.AUTOWIRE_BY_NAME,
                                                                       false);
    }

}
  • OtterContextLocator的static方法创建了ClassPathXmlApplicationContext;它提供了getOtterController方法,它从applicationContext中获取otterController

小结

OtterLauncher的main方法通过OtterContextLocator.getOtterController()获取OtterController,然后执行其start方法,之后注册shutdownHook去执行其stop方法

doc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!