本文主要研究一下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


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...


引用和评论

0 条评论