本文主要研究一下OtterLauncherjava
otter/node/deployer/src/main/java/com/alibaba/otter/node/deployer/OtterLauncher.javanode
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 // } }
otter/node/etl/src/main/java/com/alibaba/otter/node/etl/OtterContextLocator.javagit
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); } }
OtterLauncher的main方法經過OtterContextLocator.getOtterController()獲取OtterController,而後執行其start方法,以後註冊shutdownHook去執行其stop方法github