Example #1
0
  /**
   * 初始化<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);
  }
Example #2
0
  /**
   * 初始化集群<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);
  }
Example #3
0
 /** 在垃圾回收(GC)被调用时关闭MongoDB客户端 */
 @Override
 protected void finalize() throws Throwable {
   log.info("MongoDS is finalized!");
   this.close();
   super.finalize();
 }
Example #4
0
  /**
   * 构件MongoDB连接选项<br>
   *
   * @param group 分组,当分组对应的选项不存在时会读取根选项,如果也不存在使用默认值
   * @return Builder
   */
  private Builder buildMongoClientOptions(Builder builder, String group) {
    if (setting == null) {
      return builder;
    }

    if (group == null) {
      group = StrUtil.EMPTY;
    } else {
      group = group + StrUtil.DOT;
    }

    // 每个主机答应的连接数(每个主机的连接池大小),当连接池被用光时,会被阻塞住
    Integer connectionsPerHost = setting.getInt(group + "connectionsPerHost");
    if (StrUtil.isBlank(group) == false && connectionsPerHost == null) {
      connectionsPerHost = setting.getInt("connectionsPerHost");
    }
    if (connectionsPerHost != null) {
      builder.connectionsPerHost(connectionsPerHost);
      log.debug("MongoDB connectionsPerHost: {}", connectionsPerHost);
    }

    // multiplier for connectionsPerHost for # of threads that can block if connectionsPerHost is
    // 10, and threadsAllowedToBlockForConnectionMultiplier is 5, then 50 threads can block more
    // than that and an exception will be throw --int
    Integer threadsAllowedToBlockForConnectionMultiplier =
        setting.getInt(group + "threadsAllowedToBlockForConnectionMultiplier");
    if (StrUtil.isBlank(group) == false && threadsAllowedToBlockForConnectionMultiplier == null) {
      threadsAllowedToBlockForConnectionMultiplier =
          setting.getInt("threadsAllowedToBlockForConnectionMultiplier");
    }
    if (threadsAllowedToBlockForConnectionMultiplier != null) {
      builder.threadsAllowedToBlockForConnectionMultiplier(
          threadsAllowedToBlockForConnectionMultiplier);
      log.debug(
          "MongoDB threadsAllowedToBlockForConnectionMultiplier: {}",
          threadsAllowedToBlockForConnectionMultiplier);
    }

    // 被阻塞线程从连接池获取连接的最长等待时间(ms) --int
    Integer connectTimeout = setting.getInt(group + "connectTimeout");
    if (StrUtil.isBlank(group) == false && connectTimeout == null) {
      setting.getInt("connectTimeout");
    }
    if (connectTimeout != null) {
      builder.connectTimeout(connectTimeout);
      log.debug("MongoDB connectTimeout: {}", connectTimeout);
    }

    // 套接字超时时间;该值会被传递给Socket.setSoTimeout(int)。默以为0(无穷) --int
    Integer socketTimeout = setting.getInt(group + "socketTimeout");
    if (StrUtil.isBlank(group) == false && socketTimeout == null) {
      setting.getInt("socketTimeout");
    }
    if (socketTimeout != null) {
      builder.socketTimeout(socketTimeout);
      log.debug("MongoDB socketTimeout: {}", socketTimeout);
    }

    // This controls whether or not to have socket keep alive turned on (SO_KEEPALIVE). defaults to
    // false --boolean
    Boolean socketKeepAlive = setting.getBool(group + "socketKeepAlive");
    if (StrUtil.isBlank(group) == false && socketKeepAlive == null) {
      socketKeepAlive = setting.getBool("socketKeepAlive");
    }
    if (socketKeepAlive != null) {
      builder.socketKeepAlive(socketKeepAlive);
      log.debug("MongoDB socketKeepAlive: {}", socketKeepAlive);
    }

    return builder;
  }