/** * $Id: NamesrvStartup.java 1839 2013-05-16 02:12:02Z shijia.wxr $ */ package com.alibaba.rocketmq.namesrv; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.InputStream; import java.util.Properties; import java.util.concurrent.atomic.AtomicInteger; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.apache.commons.cli.PosixParser; import org.slf4j.Logger; import org.slf4j.LoggerFactory; //import org.apache.log4j.xml.DOMConfigurator; import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.joran.JoranConfigurator; import com.alibaba.rocketmq.common.MixAll; import com.alibaba.rocketmq.common.namesrv.NamesrvConfig; import com.alibaba.rocketmq.remoting.netty.NettyClientConfig; import com.alibaba.rocketmq.remoting.netty.NettyServerConfig; /** * Name server ������� * * @author vintage.wang@gmail.com shijia.wxr@taobao.com * */ public class NamesrvStartup { public static Options buildCommandlineOptions(final Options options) { Option opt = new Option("c", "configFile", true, "Name server config properties file"); opt.setRequired(false); options.addOption(opt); opt = new Option("p", "printConfigItem", false, "Print all config item"); opt.setRequired(false); options.addOption(opt); return options; } public static void main(String[] args) { try { // ���������� Options options = MixAll.buildCommandlineOptions(new Options()); final CommandLine commandLine = MixAll.parseCmdLine("mqnamesrv", args, buildCommandlineOptions(options), new PosixParser()); if (null == commandLine) { System.exit(-1); return; } // ��ʼ�������ļ� final NamesrvConfig namesrvConfig = new NamesrvConfig(); final NettyServerConfig nettyServerConfig = new NettyServerConfig(); nettyServerConfig.setListenPort(9876); final NettyClientConfig nettyClientConfig = new NettyClientConfig(); if (commandLine.hasOption('c')) { String file = commandLine.getOptionValue('c'); if (file != null) { InputStream in = new BufferedInputStream(new FileInputStream(file)); Properties properties = new Properties(); properties.load(in); MixAll.properties2Object(properties, namesrvConfig); MixAll.properties2Object(properties, nettyServerConfig); MixAll.properties2Object(properties, nettyClientConfig); System.out.println("load config properties file OK, " + file); in.close(); } } // ��ӡĬ������ if (commandLine.hasOption('p')) { MixAll.printObjectProperties(null, namesrvConfig); MixAll.printObjectProperties(null, nettyServerConfig); MixAll.printObjectProperties(null, nettyClientConfig); System.exit(0); } MixAll.properties2Object(MixAll.commandLine2Properties(commandLine), namesrvConfig); if (null == namesrvConfig.getRocketmqHome()) { System.out.println("Please set the " + MixAll.ROCKETMQ_HOME_ENV + " variable in your environment to match the location of the RocketMQ installation"); System.exit(-2); } // ��ʼ��Logback LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(lc); lc.reset(); configurator.doConfigure(namesrvConfig.getRocketmqHome() + "/conf/log4j_namesrv.xml"); final Logger log = LoggerFactory.getLogger(MixAll.NamesrvLoggerName); // ��ʼ��������ƶ��� final NamesrvController controller = new NamesrvController(namesrvConfig, nettyServerConfig, nettyClientConfig); boolean initResult = controller.initialize(); if (!initResult) { controller.shutdown(); System.exit(-3); } Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { private volatile boolean hasShutdown = false; private AtomicInteger shutdownTimes = new AtomicInteger(0); @Override public void run() { synchronized (this) { log.info("shutdown hook was invoked, " + this.shutdownTimes.incrementAndGet()); if (!this.hasShutdown) { this.hasShutdown = true; long begineTime = System.currentTimeMillis(); controller.shutdown(); long consumingTimeTotal = System.currentTimeMillis() - begineTime; log.info("shutdown hook over, consuming time total(ms): " + consumingTimeTotal); } } } }, "ShutdownHook")); // �������� controller.start(); String tip = "The Name Server boot success."; log.info(tip); System.out.println(tip); } catch (Throwable e) { e.printStackTrace(); System.exit(-1); } } }