示例#1
0
  private static void startServer(boolean fork) {
    LOGGER.info("Starting " + ansi().fg(RED).bold().a("RESTHeart").reset().toString());

    if (RESTHEART_VERSION != null) {
      LOGGER.info("version {}", RESTHEART_VERSION);
    }

    Path pidFilePath = FileUtils.getPidFilePath(FileUtils.getFileAbsoultePathHash(CONF_FILE_PATH));

    boolean pidFileAlreadyExists = false;

    if (!OSChecker.isWindows() && pidFilePath != null) {
      pidFileAlreadyExists = checkPidFile(CONF_FILE_PATH);
    }

    logLoggingConfiguration(fork);

    LOGGER.debug(
        "Initializing MongoDB connection pool to {} with options {}",
        configuration.getMongoUri().getHosts(),
        configuration.getMongoUri().getOptions());

    try {
      MongoDBClientSingleton.init(configuration);
      // force setup
      MongoDBClientSingleton.getInstance();
      LOGGER.info("MongoDB connection pool initialized");
      LOGGER.info("MongoDB version {}", MongoDBClientSingleton.getServerVersion());
    } catch (Throwable t) {
      LOGGER.error("Error connecting to MongoDB. exiting..", t);
      stopServer(false, !pidFileAlreadyExists);
      System.exit(-1);
    }

    try {
      startCoreSystem();
    } catch (Throwable t) {
      LOGGER.error("Error starting RESTHeart. Exiting...", t);
      stopServer(false, !pidFileAlreadyExists);
      System.exit(-2);
    }

    Runtime.getRuntime()
        .addShutdownHook(
            new Thread() {
              @Override
              public void run() {
                stopServer(false);
              }
            });

    // create pid file on supported OSes
    if (!OSChecker.isWindows() && pidFilePath != null) {
      FileUtils.createPidFile(pidFilePath);
    }

    // log pid file path on supported OSes
    if (!OSChecker.isWindows() && pidFilePath != null) {
      LOGGER.info("Pid file {}", pidFilePath);
    }

    LOGGER.info(ansi().fg(GREEN).bold().a("RESTHeart started").reset().toString());
  }