예제 #1
0
파일: MongoDS.java 프로젝트: luckyLz/hutool
  /**
   * 初始化<br>
   * 设定文件中的host和端口有三种形式: --------------------- host = host:port --------------------- host = host
   * port = port --------------------- #此种形式使用MongoDB默认端口 host = host ---------------------
   */
  public synchronized void initSingle() {
    if (setting == null) {
      try {
        setting = new Setting(MONGO_CONFIG_PATH, CharsetUtil.UTF_8, true);
      } catch (Exception e) {
        // 在single模式下,可以没有配置文件。
      }
    }

    String group = StrUtil.EMPTY;
    if (serverAddress == null) {
      if (groups != null && groups.length == 1) {
        group = groups[0];
      }
      serverAddress = createServerAddress(group);
    }
    try {
      mongo = new MongoClient(serverAddress, buildMongoClientOptions(group));
    } catch (Exception e) {
      throw new UtilException(
          StrUtil.format("Init MongoDB pool with connection to [{}] error!", serverAddress), e);
    }

    log.info("Init MongoDB pool with connection to [{}]", serverAddress);
  }
예제 #2
0
파일: MongoDS.java 프로젝트: luckyLz/hutool
  /**
   * 初始化集群<br>
   * 集群的其它客户端设定参数使用全局设定<br>
   * 集群中每一个实例成员用一个group表示,例如:<br>
   * [db0] host = 10.11.49.157:27117 [db1] host = 10.11.49.157:27118 [db2] host = 10.11.49.157:27119
   */
  public synchronized void initCloud() {
    if (groups == null || groups.length == 0) {
      throw new UtilException("Please give replication set groups!");
    }

    if (setting == null) {
      // 若未指定配置文件,则使用默认配置文件
      setting = new Setting(MONGO_CONFIG_PATH, Setting.DEFAULT_CHARSET, true);
    }

    List<ServerAddress> addrList = new ArrayList<ServerAddress>();
    for (String group : groups) {
      addrList.add(createServerAddress(group));
    }

    try {
      mongo = new MongoClient(addrList, buildMongoClientOptions(StrUtil.EMPTY));
    } catch (Exception e) {
      log.error("Init MongoDB connection error!", e);
      return;
    }

    log.info("Init MongoDB cloud Set pool with connection to {}", addrList);
  }
예제 #3
0
파일: MongoDS.java 프로젝트: luckyLz/hutool
 /** 在垃圾回收(GC)被调用时关闭MongoDB客户端 */
 @Override
 protected void finalize() throws Throwable {
   log.info("MongoDS is finalized!");
   this.close();
   super.finalize();
 }