Exemplo n.º 1
0
  public MapSolrStore(String mapName, Properties properties) {
    _mapName = mapName;
    _properties = properties;

    try {
      if (_properties.getProperty(SolrTools.SOLR_SERVER_URLS) == null) {
        throw new RuntimeException(
            "propertie Solr '" + SolrTools.SOLR_SERVER_URLS + "' Can not null");
      }
      _solrServerUrls = _properties.getProperty(SolrTools.SOLR_SERVER_URLS);
      if (_properties.getProperty(SolrTools.CORE_NAME) != null) {
        _coreName = _properties.getProperty(SolrTools.CORE_NAME);
      }
      if (_properties.getProperty(SolrTools.CONNECT_TIMEOUT) != null) {
        _connectTimeout =
            Integer.parseInt(_properties.getProperty(SolrTools.CONNECT_TIMEOUT)) * 1000;
      }
      if (_properties.getProperty(SolrTools.READ_TIMEOUT) != null) {
        _readTimeout = Integer.parseInt(_properties.getProperty(SolrTools.READ_TIMEOUT)) * 1000;
      }
      if (_properties.getProperty(SolrTools.DELETE_ON_EVICT) != null) {
        _deleteOnEvict = Boolean.parseBoolean(_properties.getProperty(SolrTools.DELETE_ON_EVICT));
      }
      if (_properties.getProperty(SolrTools.LOAD_ALL) != null) {
        _loadAll = Boolean.parseBoolean(_properties.getProperty(SolrTools.LOAD_ALL));
      }

      _stateArray =
          SolrTools.getClusterState(_solrServerUrls, _coreName, _connectTimeout, _readTimeout);
      if (_stateArray == null) {
        throw new RuntimeException(
            "can not connect Solr Cloud:" + "coreName:" + _coreName + ",URLS:" + _solrServerUrls);
      } else {
        _logger.log(Level.INFO, "Solr Cloud Status:" + _stateArray.encodePrettily());
      }

      this._urlGets = new java.util.ArrayList<String>(_stateArray.size());
      this._urlUpdates = new java.util.ArrayList<String>(_stateArray.size());
      this._urlSelects = new java.util.ArrayList<String>(_stateArray.size());
      for (int i = 0; i < _stateArray.size(); i++) {
        JsonObject jNode = _stateArray.<JsonObject>get(i);
        if (jNode.getString("state").equalsIgnoreCase("active")
            || jNode.getString("state").equalsIgnoreCase("recovering")) {
          this._urlGets.add(jNode.getString("base_url") + "/" + _coreName + "/get?id=");
          this._urlUpdates.add(jNode.getString("base_url") + "/" + _coreName + "/update");
          this._urlSelects.add(jNode.getString("base_url") + "/" + _coreName + "/select");
        }
      }
    } catch (Exception ex) {
      this._urlGets = null;
      this._urlUpdates = null;
      this._urlSelects = null;
      _logger.log(Level.WARNING, ex.getMessage(), ex);
    }
  }
Exemplo n.º 2
0
  @Override
  // 刷新Solr集群状态的Scheduled
  public void run() {
    JsonArray stateArray =
        SolrTools.getClusterState(_solrServerUrls, _coreName, _connectTimeout, _readTimeout);
    if (stateArray == null) {
      _logger.log(
          Level.WARNING,
          "can not connect Solr Cloud:" + "coreName:" + _coreName + ",URLS:" + _solrServerUrls);
      return;
    }
    if (_stateArray.encode().equals(stateArray.encode())) {
      return;
    }
    _stateArray = stateArray;

    java.util.List<String> newUrlGets = new java.util.ArrayList<String>(stateArray.size());
    java.util.List<String> newUrlUpdates = new java.util.ArrayList<String>(stateArray.size());
    java.util.List<String> newUrlSelects = new java.util.ArrayList<String>(stateArray.size());
    for (int i = 0; i < stateArray.size(); i++) {
      JsonObject jj = stateArray.<JsonObject>get(i);
      if (jj.getString("state").equalsIgnoreCase("active")
          || jj.getString("state").equalsIgnoreCase("recovering")) {
        newUrlGets.add(jj.getString("base_url") + "/" + _coreName + "/get?id=");
        newUrlUpdates.add(jj.getString("base_url") + "/" + _coreName + "/update");
        newUrlSelects.add(jj.getString("base_url") + "/" + _coreName + "/select");
      }
    }

    _lockGet.lock();
    try {
      this._urlGets.clear();
      this._urlGets = newUrlGets;
    } finally {
      _lockGet.unlock();
    }

    _lockPost.lock();
    try {
      this._urlUpdates.clear();
      this._urlUpdates = newUrlUpdates;
    } finally {
      _lockPost.unlock();
    }

    _lockSelect.lock();
    try {
      this._urlSelects.clear();
      this._urlSelects = newUrlSelects;
    } finally {
      _lockSelect.unlock();
    }
  }