lizhanhui closed pull request #243: [ROCKETMQ-387]Standardize the startup class 
structure
URL: https://github.com/apache/rocketmq/pull/243
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/broker/src/main/java/org/apache/rocketmq/broker/BrokerStartup.java 
b/broker/src/main/java/org/apache/rocketmq/broker/BrokerStartup.java
index 7627bb967..f0a115012 100644
--- a/broker/src/main/java/org/apache/rocketmq/broker/BrokerStartup.java
+++ b/broker/src/main/java/org/apache/rocketmq/broker/BrokerStartup.java
@@ -79,6 +79,12 @@ public static BrokerController start(BrokerController 
controller) {
         return null;
     }
 
+    public static void shutdown(final BrokerController controller) {
+        if (null != controller) {
+            controller.shutdown();
+        }
+    }
+
     public static BrokerController createBrokerController(String[] args) {
         System.setProperty(RemotingCommand.REMOTING_VERSION_KEY, 
Integer.toString(MQVersion.CURRENT_VERSION));
 
diff --git 
a/namesrv/src/main/java/org/apache/rocketmq/namesrv/NamesrvStartup.java 
b/namesrv/src/main/java/org/apache/rocketmq/namesrv/NamesrvStartup.java
index a55735a9b..a073dafb2 100644
--- a/namesrv/src/main/java/org/apache/rocketmq/namesrv/NamesrvStartup.java
+++ b/namesrv/src/main/java/org/apache/rocketmq/namesrv/NamesrvStartup.java
@@ -18,8 +18,10 @@
 
 import ch.qos.logback.classic.LoggerContext;
 import ch.qos.logback.classic.joran.JoranConfigurator;
+import ch.qos.logback.core.joran.spi.JoranException;
 import java.io.BufferedInputStream;
 import java.io.FileInputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.util.Properties;
 import java.util.concurrent.Callable;
@@ -40,99 +42,121 @@
 import org.slf4j.LoggerFactory;
 
 public class NamesrvStartup {
-    public static Properties properties = null;
-    public static CommandLine commandLine = null;
+
+    private static InternalLogger log;
+    private static Properties properties = null;
+    private static CommandLine commandLine = null;
 
     public static void main(String[] args) {
         main0(args);
     }
 
     public static NamesrvController main0(String[] args) {
-        System.setProperty(RemotingCommand.REMOTING_VERSION_KEY, 
Integer.toString(MQVersion.CURRENT_VERSION));
+
         try {
-            //PackageConflictDetect.detectFastjson();
+            NamesrvController controller = createNamesrvController(args);
+            start(controller);
+            String tip = "The Name Server boot success. serializeType=" + 
RemotingCommand.getSerializeTypeConfigInThisServer();
+            log.info(tip);
+            System.out.printf("%s%n", tip);
+            return controller;
+        } catch (Throwable e) {
+            e.printStackTrace();
+            System.exit(-1);
+        }
 
-            Options options = ServerUtil.buildCommandlineOptions(new 
Options());
-            commandLine = ServerUtil.parseCmdLine("mqnamesrv", args, 
buildCommandlineOptions(options), new PosixParser());
-            if (null == commandLine) {
-                System.exit(-1);
-                return null;
-            }
+        return null;
+    }
 
-            final NamesrvConfig namesrvConfig = new NamesrvConfig();
-            final NettyServerConfig nettyServerConfig = new 
NettyServerConfig();
-            nettyServerConfig.setListenPort(9876);
-            if (commandLine.hasOption('c')) {
-                String file = commandLine.getOptionValue('c');
-                if (file != null) {
-                    InputStream in = new BufferedInputStream(new 
FileInputStream(file));
-                    properties = new Properties();
-                    properties.load(in);
-                    MixAll.properties2Object(properties, namesrvConfig);
-                    MixAll.properties2Object(properties, nettyServerConfig);
-
-                    namesrvConfig.setConfigStorePath(file);
-
-                    System.out.printf("load config properties file OK, %s%n", 
file);
-                    in.close();
-                }
-            }
+    public static NamesrvController createNamesrvController(String[] args) 
throws IOException, JoranException {
+        System.setProperty(RemotingCommand.REMOTING_VERSION_KEY, 
Integer.toString(MQVersion.CURRENT_VERSION));
+        //PackageConflictDetect.detectFastjson();
 
-            if (commandLine.hasOption('p')) {
-                MixAll.printObjectProperties(null, namesrvConfig);
-                MixAll.printObjectProperties(null, nettyServerConfig);
-                System.exit(0);
+        Options options = ServerUtil.buildCommandlineOptions(new Options());
+        commandLine = ServerUtil.parseCmdLine("mqnamesrv", args, 
buildCommandlineOptions(options), new PosixParser());
+        if (null == commandLine) {
+            System.exit(-1);
+            return null;
+        }
+
+        final NamesrvConfig namesrvConfig = new NamesrvConfig();
+        final NettyServerConfig nettyServerConfig = new NettyServerConfig();
+        nettyServerConfig.setListenPort(9876);
+        if (commandLine.hasOption('c')) {
+            String file = commandLine.getOptionValue('c');
+            if (file != null) {
+                InputStream in = new BufferedInputStream(new 
FileInputStream(file));
+                properties = new Properties();
+                properties.load(in);
+                MixAll.properties2Object(properties, namesrvConfig);
+                MixAll.properties2Object(properties, nettyServerConfig);
+
+                namesrvConfig.setConfigStorePath(file);
+
+                System.out.printf("load config properties file OK, %s%n", 
file);
+                in.close();
             }
+        }
+
+        if (commandLine.hasOption('p')) {
+            MixAll.printObjectProperties(null, namesrvConfig);
+            MixAll.printObjectProperties(null, nettyServerConfig);
+            System.exit(0);
+        }
 
-            
MixAll.properties2Object(ServerUtil.commandLine2Properties(commandLine), 
namesrvConfig);
+        
MixAll.properties2Object(ServerUtil.commandLine2Properties(commandLine), 
namesrvConfig);
 
-            if (null == namesrvConfig.getRocketmqHome()) {
-                System.out.printf("Please set the %s variable in your 
environment to match the location of the RocketMQ installation%n", 
MixAll.ROCKETMQ_HOME_ENV);
-                System.exit(-2);
-            }
+        if (null == namesrvConfig.getRocketmqHome()) {
+            System.out.printf("Please set the %s variable in your environment 
to match the location of the RocketMQ installation%n", 
MixAll.ROCKETMQ_HOME_ENV);
+            System.exit(-2);
+        }
 
-            LoggerContext lc = (LoggerContext) 
LoggerFactory.getILoggerFactory();
-            JoranConfigurator configurator = new JoranConfigurator();
-            configurator.setContext(lc);
-            lc.reset();
-            configurator.doConfigure(namesrvConfig.getRocketmqHome() + 
"/conf/logback_namesrv.xml");
-            final InternalLogger log = 
InternalLoggerFactory.getLogger(LoggerName.NAMESRV_LOGGER_NAME);
+        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
+        JoranConfigurator configurator = new JoranConfigurator();
+        configurator.setContext(lc);
+        lc.reset();
+        configurator.doConfigure(namesrvConfig.getRocketmqHome() + 
"/conf/logback_namesrv.xml");
 
-            MixAll.printObjectProperties(log, namesrvConfig);
-            MixAll.printObjectProperties(log, nettyServerConfig);
+        log = InternalLoggerFactory.getLogger(LoggerName.NAMESRV_LOGGER_NAME);
 
-            final NamesrvController controller = new 
NamesrvController(namesrvConfig, nettyServerConfig);
+        MixAll.printObjectProperties(log, namesrvConfig);
+        MixAll.printObjectProperties(log, nettyServerConfig);
 
-            // remember all configs to prevent discard
-            controller.getConfiguration().registerConfig(properties);
+        final NamesrvController controller = new 
NamesrvController(namesrvConfig, nettyServerConfig);
 
-            boolean initResult = controller.initialize();
-            if (!initResult) {
-                controller.shutdown();
-                System.exit(-3);
-            }
+        // remember all configs to prevent discard
+        controller.getConfiguration().registerConfig(properties);
 
-            Runtime.getRuntime().addShutdownHook(new ShutdownHookThread(log, 
new Callable<Void>() {
-                @Override
-                public Void call() throws Exception {
-                    controller.shutdown();
-                    return null;
-                }
-            }));
+        return controller;
+    }
 
-            controller.start();
+    public static NamesrvController start(final NamesrvController controller) 
throws Exception {
 
-            String tip = "The Name Server boot success. serializeType=" + 
RemotingCommand.getSerializeTypeConfigInThisServer();
-            log.info(tip);
-            System.out.printf("%s%n", tip);
+        if (null == controller) {
+            throw new IllegalArgumentException("NamesrvController is null");
+        }
 
-            return controller;
-        } catch (Throwable e) {
-            e.printStackTrace();
-            System.exit(-1);
+        boolean initResult = controller.initialize();
+        if (!initResult) {
+            controller.shutdown();
+            System.exit(-3);
         }
 
-        return null;
+        Runtime.getRuntime().addShutdownHook(new ShutdownHookThread(log, new 
Callable<Void>() {
+            @Override
+            public Void call() throws Exception {
+                controller.shutdown();
+                return null;
+            }
+        }));
+
+        controller.start();
+
+        return controller;
+    }
+
+    public static void shutdown(final NamesrvController controller) {
+        controller.shutdown();
     }
 
     public static Options buildCommandlineOptions(final Options options) {


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to