PageRenderTime 60ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/rocketmq-filtersrv/src/main/java/com/alibaba/rocketmq/filtersrv/FiltersrvStartup.java

https://gitlab.com/xialeizhou/RocketMQ
Java | 207 lines | 139 code | 37 blank | 31 comment | 16 complexity | c3a66884c4ad7dea87cbaa2d1f4ad162 MD5 | raw file
  1. /**
  2. * Copyright (C) 2010-2013 Alibaba Group Holding Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.alibaba.rocketmq.filtersrv;
  17. import java.io.BufferedInputStream;
  18. import java.io.FileInputStream;
  19. import java.io.InputStream;
  20. import java.util.Properties;
  21. import java.util.concurrent.atomic.AtomicInteger;
  22. import org.apache.commons.cli.CommandLine;
  23. import org.apache.commons.cli.Option;
  24. import org.apache.commons.cli.Options;
  25. import org.apache.commons.cli.PosixParser;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;
  28. import ch.qos.logback.classic.LoggerContext;
  29. import ch.qos.logback.classic.joran.JoranConfigurator;
  30. import com.alibaba.rocketmq.common.MQVersion;
  31. import com.alibaba.rocketmq.common.MixAll;
  32. import com.alibaba.rocketmq.common.conflict.PackageConflictDetect;
  33. import com.alibaba.rocketmq.common.constant.LoggerName;
  34. import com.alibaba.rocketmq.remoting.netty.NettyServerConfig;
  35. import com.alibaba.rocketmq.remoting.netty.NettySystemConfig;
  36. import com.alibaba.rocketmq.remoting.protocol.RemotingCommand;
  37. import com.alibaba.rocketmq.srvutil.ServerUtil;
  38. /**
  39. * Filter server 启动入口
  40. *
  41. * @author shijia.wxr<vintage.wang@gmail.com>
  42. * @since 2014-4-10
  43. */
  44. public class FiltersrvStartup {
  45. public static Logger log;
  46. public static Options buildCommandlineOptions(final Options options) {
  47. Option opt = new Option("c", "configFile", true, "Filter server config properties file");
  48. opt.setRequired(false);
  49. options.addOption(opt);
  50. opt = new Option("p", "printConfigItem", false, "Print all config item");
  51. opt.setRequired(false);
  52. options.addOption(opt);
  53. return options;
  54. }
  55. public static void main(String[] args) {
  56. start(createController(args));
  57. }
  58. public static FiltersrvController start(FiltersrvController controller) {
  59. // 启动服务
  60. try {
  61. controller.start();
  62. }
  63. catch (Exception e) {
  64. e.printStackTrace();
  65. System.exit(-1);
  66. }
  67. String tip = "The Filter Server boot success, " + controller.localAddr();
  68. log.info(tip);
  69. System.out.println(tip);
  70. return controller;
  71. }
  72. public static FiltersrvController createController(String[] args) {
  73. System.setProperty(RemotingCommand.RemotingVersionKey, Integer.toString(MQVersion.CurrentVersion));
  74. // Socket发送缓冲区大小
  75. if (null == System.getProperty(NettySystemConfig.SystemPropertySocketSndbufSize)) {
  76. NettySystemConfig.SocketSndbufSize = 65535;
  77. }
  78. // Socket接收缓冲区大小
  79. if (null == System.getProperty(NettySystemConfig.SystemPropertySocketRcvbufSize)) {
  80. NettySystemConfig.SocketRcvbufSize = 1024;
  81. }
  82. try {
  83. // 检测包冲突
  84. PackageConflictDetect.detectFastjson();
  85. // 解析命令行
  86. Options options = ServerUtil.buildCommandlineOptions(new Options());
  87. final CommandLine commandLine =
  88. ServerUtil.parseCmdLine("mqfiltersrv", args, buildCommandlineOptions(options),
  89. new PosixParser());
  90. if (null == commandLine) {
  91. System.exit(-1);
  92. return null;
  93. }
  94. // 初始化配置文件
  95. final FiltersrvConfig filtersrvConfig = new FiltersrvConfig();
  96. final NettyServerConfig nettyServerConfig = new NettyServerConfig();
  97. if (commandLine.hasOption('c')) {
  98. String file = commandLine.getOptionValue('c');
  99. if (file != null) {
  100. InputStream in = new BufferedInputStream(new FileInputStream(file));
  101. Properties properties = new Properties();
  102. properties.load(in);
  103. MixAll.properties2Object(properties, filtersrvConfig);
  104. System.out.println("load config properties file OK, " + file);
  105. in.close();
  106. String port = properties.getProperty("listenPort");
  107. if (port != null) {
  108. filtersrvConfig.setConnectWhichBroker(String.format("127.0.0.1:%s", port));
  109. }
  110. }
  111. }
  112. // 强制设置为0,自动分配端口号
  113. nettyServerConfig.setListenPort(0);
  114. nettyServerConfig.setServerAsyncSemaphoreValue(filtersrvConfig.getFsServerAsyncSemaphoreValue());
  115. nettyServerConfig.setServerCallbackExecutorThreads(filtersrvConfig
  116. .getFsServerCallbackExecutorThreads());
  117. nettyServerConfig.setServerWorkerThreads(filtersrvConfig.getFsServerWorkerThreads());
  118. // 打印默认配置
  119. if (commandLine.hasOption('p')) {
  120. MixAll.printObjectProperties(null, filtersrvConfig);
  121. MixAll.printObjectProperties(null, nettyServerConfig);
  122. System.exit(0);
  123. }
  124. MixAll.properties2Object(ServerUtil.commandLine2Properties(commandLine), filtersrvConfig);
  125. if (null == filtersrvConfig.getRocketmqHome()) {
  126. System.out.println("Please set the " + MixAll.ROCKETMQ_HOME_ENV
  127. + " variable in your environment to match the location of the RocketMQ installation");
  128. System.exit(-2);
  129. }
  130. // 初始化Logback
  131. LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
  132. JoranConfigurator configurator = new JoranConfigurator();
  133. configurator.setContext(lc);
  134. lc.reset();
  135. configurator.doConfigure(filtersrvConfig.getRocketmqHome() + "/conf/logback_filtersrv.xml");
  136. log = LoggerFactory.getLogger(LoggerName.FiltersrvLoggerName);
  137. // 初始化服务控制对象
  138. final FiltersrvController controller =
  139. new FiltersrvController(filtersrvConfig, nettyServerConfig);
  140. boolean initResult = controller.initialize();
  141. if (!initResult) {
  142. controller.shutdown();
  143. System.exit(-3);
  144. }
  145. Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
  146. private volatile boolean hasShutdown = false;
  147. private AtomicInteger shutdownTimes = new AtomicInteger(0);
  148. @Override
  149. public void run() {
  150. synchronized (this) {
  151. log.info("shutdown hook was invoked, " + this.shutdownTimes.incrementAndGet());
  152. if (!this.hasShutdown) {
  153. this.hasShutdown = true;
  154. long begineTime = System.currentTimeMillis();
  155. controller.shutdown();
  156. long consumingTimeTotal = System.currentTimeMillis() - begineTime;
  157. log.info("shutdown hook over, consuming time total(ms): " + consumingTimeTotal);
  158. }
  159. }
  160. }
  161. }, "ShutdownHook"));
  162. return controller;
  163. }
  164. catch (Throwable e) {
  165. e.printStackTrace();
  166. System.exit(-1);
  167. }
  168. return null;
  169. }
  170. }