聊聊OtterLauncher

编程

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

{}",

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:

{}",

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

  • OtterLauncher

以上是 聊聊OtterLauncher 的全部内容, 来源链接: utcz.com/z/517272.html

回到顶部