前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊OtterLauncher

聊聊OtterLauncher

作者头像
code4it
发布2020-06-17 17:16:26
3510
发布2020-06-17 17:16:26
举报
文章被收录于专栏:码匠的流水账

本文主要研究一下OtterLauncher

OtterLauncher

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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

  • OtterLauncher
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-06-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • OtterLauncher
  • OtterContextLocator
  • 小结
  • doc
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档