org.elasticsearch.client.RestClientBuilder.setRequestConfigCallback()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(15.3k)|赞(0)|评价(0)|浏览(202)

本文整理了Java中org.elasticsearch.client.RestClientBuilder.setRequestConfigCallback方法的一些代码示例,展示了RestClientBuilder.setRequestConfigCallback的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。RestClientBuilder.setRequestConfigCallback方法的具体详情如下:
包路径:org.elasticsearch.client.RestClientBuilder
类名称:RestClientBuilder
方法名:setRequestConfigCallback

RestClientBuilder.setRequestConfigCallback介绍

[英]Sets the RequestConfigCallback to be used to customize http client configuration
[中]

代码示例

代码示例来源:origin: apache/incubator-gobblin

builder.setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder
  .setConnectionRequestTimeout(0)); // Important, otherwise the client has spurious timeouts

代码示例来源:origin: apache/nifi

.setRequestConfigCallback(requestConfigBuilder -> {
  requestConfigBuilder.setConnectTimeout(connectTimeout);
  requestConfigBuilder.setSocketTimeout(readTimeout);

代码示例来源:origin: apache/storm

/**
   * Creates a new {@link RestClient} using given {@link EsConfig}.
   *
   * @return {@link RestClient} for Elasticsearch connection
   */
  public RestClient construct() {
    RestClientBuilder builder = RestClient.builder(esConfig.getHttpHosts());
    if (esConfig.getMaxRetryTimeoutMillis() != null) {
      builder.setMaxRetryTimeoutMillis(esConfig.getMaxRetryTimeoutMillis());
    }
    if (esConfig.getDefaultHeaders() != null) {
      builder.setDefaultHeaders(esConfig.getDefaultHeaders());
    }
    if (esConfig.getFailureListener() != null) {
      builder.setFailureListener(esConfig.getFailureListener());
    }
    if (esConfig.getHttpClientConfigCallback() != null) {
      builder.setHttpClientConfigCallback(esConfig.getHttpClientConfigCallback());
    }
    if (esConfig.getRequestConfigCallback() != null) {
      builder.setRequestConfigCallback(esConfig.getRequestConfigCallback());
    }
    if (esConfig.getPathPrefix() != null) {
      builder.setPathPrefix(esConfig.getPathPrefix());
    }
    return builder.build();
  }
}

代码示例来源:origin: apache/metron

/**
 * Creates an Elasticsearch client from settings provided via the global config.
 *
 * @return new client
 */
public static ElasticsearchClient create(Map<String, Object> globalConfig) {
 ElasticsearchClientConfig esClientConfig = new ElasticsearchClientConfig(
   getEsSettings(globalConfig));
 HttpHost[] httpHosts = getHttpHosts(globalConfig, esClientConfig.getConnectionScheme());
 RestClientBuilder builder = RestClient.builder(httpHosts);
 builder.setRequestConfigCallback(reqConfigBuilder -> {
  // Modifies request config builder with connection and socket timeouts.
  // https://www.elastic.co/guide/en/elasticsearch/client/java-rest/5.6/_timeouts.html
  reqConfigBuilder.setConnectTimeout(esClientConfig.getConnectTimeoutMillis());
  reqConfigBuilder.setSocketTimeout(esClientConfig.getSocketTimeoutMillis());
  return reqConfigBuilder;
 });
 builder.setMaxRetryTimeoutMillis(esClientConfig.getMaxRetryTimeoutMillis());
 builder.setHttpClientConfigCallback(clientBuilder -> {
  clientBuilder.setDefaultIOReactorConfig(getIOReactorConfig(esClientConfig));
  clientBuilder.setDefaultCredentialsProvider(getCredentialsProvider(esClientConfig));
  clientBuilder.setSSLContext(getSSLContext(esClientConfig));
  return clientBuilder;
 });
 RestClient lowLevelClient = builder.build();
 RestHighLevelClient client = new RestHighLevelClient(lowLevelClient);
 return new ElasticsearchClient(lowLevelClient, client);
}

代码示例来源:origin: tmobile/pacbot

private RestClient getRestClient() {
  if (restClient == null) {
    RestClientBuilder builder = RestClient.builder(new HttpHost(esHost, esPort));
    builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
      @Override
      public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
        return requestConfigBuilder.setConnectionRequestTimeout(0);
      }
    });
    restClient = builder.build();
  }
  return restClient;
}

代码示例来源:origin: ViDA-NYU/ache

public RestClient createRestClient(ElasticSearchConfig config) {
  List<String> esHosts = config.getRestApiHosts();
  List<HttpHost> hosts = new ArrayList<>();
  for (String host : esHosts) {
    try {
      URL url = new URL(host);
      hosts.add(new HttpHost(url.getHost(), url.getPort()));
    } catch (MalformedURLException e) {
      throw new RuntimeException("Failed to initialize Elasticsearch REST client. "
          + "Invalid host: " + host, e);
    }
  }
  HttpHost[] httpHostsArray = (HttpHost[]) hosts.toArray(new HttpHost[hosts.size()]);
  
  client = RestClient.builder(httpHostsArray)
    .setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
      @Override
      public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
        return requestConfigBuilder
            .setConnectTimeout(config.getRestConnectTimeout())
            .setSocketTimeout(config.getRestSocketTimeout());
      }
    })
    .setMaxRetryTimeoutMillis(config.getRestMaxRetryTimeoutMillis())
    .build();
  
  logger.info("Initialized Elasticsearch REST client for hosts: "+Arrays.toString(httpHostsArray));
  return client;
}

代码示例来源:origin: com.scireum/sirius-db

private synchronized void initializeClient() {
  if (client == null) {
    Elastic.LOG.INFO("Initializing Elasticsearch client against: %s", hosts);
    // Fixes an Elastic bug that results in TimeoutExceptions
    // Remove this, once ES is updated to at least 6.3.1
    RestClientBuilder.RequestConfigCallback configCallback =
        requestConfigBuilder -> requestConfigBuilder.setConnectionRequestTimeout(0);
    client = new LowLevelClient(RestClient.builder(Arrays.stream(hosts.split(","))
                               .map(String::trim)
                               .map(host -> Strings.splitAtLast(host, ":"))
                               .map(this::parsePort)
                               .map(this::mapPort)
                               .map(this::makeHttpHost)
                               .toArray(size -> new HttpHost[size]))
                       .setRequestConfigCallback(configCallback)
                       .build());
    // If we're using a docker container (most probably for testing), we give ES some time
    // to fully boot up. Otherwise strange connection issues might arise.
    if (dockerDetected) {
      waitForElasticsearchToBecomReady();
    }
  }
}

代码示例来源:origin: tmobile/pacbot

private RestClient getRestClient() {
    if (restClient == null) {
      String esHost = config.getElasticSearch().getDevIngestHost();
      int esPort = config.getElasticSearch().getDevIngestPort();
      RestClientBuilder builder = RestClient.builder(new HttpHost(esHost, esPort));
      RequestConfigCallback requestConfigCallback = requestConfigBuilder -> requestConfigBuilder
          .setConnectionRequestTimeout(0);
      builder.setRequestConfigCallback(requestConfigCallback);
      restClient = builder.build();
    }
    return restClient;
  }
}

代码示例来源:origin: ViDA-NYU/ache

public static RestClient createClient(ElasticSearchConfig config) {
  HttpHost[] httpHosts = parseHostAddresses(config.getRestApiHosts());
  RestClient client = RestClient.builder(httpHosts)
      .setRequestConfigCallback(new RequestConfigCallback() {
        @Override
        public Builder customizeRequestConfig(
            Builder requestConfigBuilder) {
          return requestConfigBuilder
              .setConnectTimeout(config.getRestConnectTimeout())
              .setSocketTimeout(config.getRestSocketTimeout());
        }
      })
      .setMaxRetryTimeoutMillis(config.getRestMaxRetryTimeoutMillis())
      .build();
  logger.info("Initialized Elasticsearch REST client for: " + Arrays.toString(httpHosts));
  return client;
}

代码示例来源:origin: tmobile/pacbot

private RestClient getRestClient() {
  if (restClient == null) {
    String esHost = config.getElasticSearch().getDevIngestHost();
    int esPort = config.getElasticSearch().getDevIngestPort();
    RestClientBuilder builder = RestClient.builder(new HttpHost(esHost, esPort));
    builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
      @Override
      public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
        return requestConfigBuilder.setConnectionRequestTimeout(0);
      }
    });
    restClient = builder.build();
  }
  return restClient;
}

代码示例来源:origin: tmobile/pacbot

private RestClient getRestClient() {
  if (restClient == null) {
    String esHost = config.getElasticSearch().getDevIngestHost();
    int esPort = config.getElasticSearch().getDevIngestPort();
    RestClientBuilder builder = RestClient.builder(new HttpHost(esHost, esPort));
    builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
      @Override
      public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
        return requestConfigBuilder.setConnectionRequestTimeout(0);
      }
    });
    restClient = builder.build();
  }
  return restClient;
}

代码示例来源:origin: delirius325/jmeter-elasticsearch-backend-listener

this.buildNumber = (JMeterUtils.getProperty(ElasticsearchBackendClient.BUILD_NUMBER) != null && !JMeterUtils.getProperty(ElasticsearchBackendClient.BUILD_NUMBER).trim().equals("")) ? Integer.parseInt(JMeterUtils.getProperty(ElasticsearchBackendClient.BUILD_NUMBER)) : 0;
RestClient client = RestClient.builder(new HttpHost(context.getParameter(ES_HOST), Integer.parseInt(context.getParameter(ES_PORT)), context.getParameter(ES_SCHEME)))
    .setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder.setConnectTimeout(5000)
    .setSocketTimeout((int) timeoutMs))
    .setFailureListener(new RestClient.FailureListener() {

代码示例来源:origin: com.strapdata.elasticsearch.test/framework

if (socketTimeoutString != null) {
  final TimeValue socketTimeout = TimeValue.parseTimeValue(socketTimeoutString, CLIENT_SOCKET_TIMEOUT);
  builder.setRequestConfigCallback(conf -> conf.setSocketTimeout(Math.toIntExact(socketTimeout.getMillis())));

代码示例来源:origin: org.nuxeo.elasticsearch/nuxeo-elasticsearch-core

protected ESClient createRestClient(ElasticSearchClientConfig config) {
  String addressList = config.getOption("addressList", "");
  if (addressList.isEmpty()) {
    throw new IllegalArgumentException("No addressList option provided cannot connect RestClient");
  }
  String[] hosts = addressList.split(",");
  HttpHost[] httpHosts = new HttpHost[hosts.length];
  int i = 0;
  for (String host : hosts) {
    httpHosts[i++] = HttpHost.create(host);
  }
  RestClientBuilder builder = RestClient.builder(httpHosts)
                     .setRequestConfigCallback(
                         requestConfigBuilder -> requestConfigBuilder.setConnectTimeout(
                             getConnectTimeoutMs(config))
                                               .setSocketTimeout(
                                                   getSocketTimeoutMs(
                                                       config)))
                     .setMaxRetryTimeoutMillis(getConnectTimeoutMs(config));
  addClientCallback(config, builder);
  RestHighLevelClient client = new RestHighLevelClient(builder); // NOSONAR (factory)
  // checkConnection(client);
  return new ESRestClient(client.getLowLevelClient(), client);
}

代码示例来源:origin: org.apache.camel/camel-elasticsearch-rest

private RestClient createClient() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
  final RestClientBuilder builder = RestClient.builder(configuration.getHostAddressesList().toArray(new HttpHost[0]));
  builder.setMaxRetryTimeoutMillis(configuration.getMaxRetryTimeout());
  builder.setRequestConfigCallback(requestConfigBuilder ->
    requestConfigBuilder.setConnectTimeout(configuration.getConnectionTimeout()).setSocketTimeout(configuration.getSocketTimeout()));
  if (configuration.getUser() != null && configuration.getPassword() != null) {
    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(configuration.getUser(), configuration.getPassword()));
    builder.setHttpClientConfigCallback(httpClientBuilder -> {
      httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
      return httpClientBuilder;
    });
  }
  final RestClient restClient = builder.build();
  if (configuration.getEnableSniffer()) {
    SnifferBuilder snifferBuilder = Sniffer.builder(restClient);
    snifferBuilder.setSniffIntervalMillis(configuration.getSnifferInterval());
    snifferBuilder.setSniffAfterFailureDelayMillis(configuration.getSniffAfterFailureDelay());
    sniffer = snifferBuilder.build();
  }
  return restClient;
}

代码示例来源:origin: org.janusgraph/janusgraph-es

protected RestClient getRestClient(HttpHost[] hosts, Configuration config) {
  final RestClientBuilder restClientBuilder = getRestClientBuilder(hosts);
  final HttpClientConfigCallback httpClientConfigCallback = getHttpClientConfigCallback(config);
  if (httpClientConfigCallback != null) {
    restClientBuilder.setHttpClientConfigCallback(httpClientConfigCallback);
  }
  final RequestConfigCallback requestConfigCallback = getRequestConfigCallback(config);
  if (requestConfigCallback != null) {
    restClientBuilder.setRequestConfigCallback(requestConfigCallback);
  }
  if (config.has(ElasticSearchIndex.MAX_RETRY_TIMEOUT)) {
    restClientBuilder.setMaxRetryTimeoutMillis(config.get(ElasticSearchIndex.MAX_RETRY_TIMEOUT));
  }
  return restClientBuilder.build();
}

代码示例来源:origin: org.hibernate.search/hibernate-search-backend-elasticsearch

private RestClient createClient(ConfigurationPropertySource propertySource, int maxRetryTimeoutMillis) {
  ServerUris hosts = ServerUris.fromStrings( HOST.get( propertySource ) );
  return RestClient.builder( hosts.asHostsArray() )
      /*
       * Note: this timeout is currently only used on retries,
       * but should we start using the synchronous methods of RestClient,
       * it would be applied to synchronous requests too.
       * See https://github.com/elastic/elasticsearch/issues/21789#issuecomment-287399115
       */
      .setMaxRetryTimeoutMillis( maxRetryTimeoutMillis )
      .setRequestConfigCallback( b -> customizeRequestConfig( propertySource, b ) )
      .setHttpClientConfigCallback( b -> customizeHttpClientConfig( propertySource, hosts, b ) )
      .build();
}

代码示例来源:origin: hibernate/hibernate-search

private RestClient createClient(ConfigurationPropertySource propertySource, int maxRetryTimeoutMillis) {
  ServerUris hosts = ServerUris.fromStrings( HOST.get( propertySource ) );
  return RestClient.builder( hosts.asHostsArray() )
      /*
       * Note: this timeout is currently only used on retries,
       * but should we start using the synchronous methods of RestClient,
       * it would be applied to synchronous requests too.
       * See https://github.com/elastic/elasticsearch/issues/21789#issuecomment-287399115
       */
      .setMaxRetryTimeoutMillis( maxRetryTimeoutMillis )
      .setRequestConfigCallback( b -> customizeRequestConfig( propertySource, b ) )
      .setHttpClientConfigCallback( b -> customizeHttpClientConfig( propertySource, hosts, b ) )
      .build();
}

代码示例来源:origin: hibernate/hibernate-search

private RestClient createClient(Properties properties, int maxRetryTimeoutMillis) {
  String serverUrisString = ConfigurationParseHelper.getString(
      properties,
      ElasticsearchEnvironment.SERVER_URI,
      ElasticsearchEnvironment.Defaults.SERVER_URI
  );
  ServerUris hosts = ServerUris.fromString( serverUrisString );
  String pathPrefix = ConfigurationParseHelper.getString( properties, ElasticsearchEnvironment.PATH_PREFIX, null );
  RestClientBuilder restClientBuilder = RestClient.builder( hosts.asHostsArray() )
      /*
       * Note: this timeout is currently only used on retries,
       * but should we start using the synchronous methods of RestClient,
       * it would be applied to synchronous requests too.
       * See https://github.com/elastic/elasticsearch/issues/21789#issuecomment-287399115
       */
      .setMaxRetryTimeoutMillis( maxRetryTimeoutMillis )
      .setRequestConfigCallback( (b) -> customizeRequestConfig( properties, b ) )
      .setHttpClientConfigCallback( (b) -> customizeHttpClientConfig( properties, hosts, b ) );
  if ( !StringHelper.isEmpty( pathPrefix ) && !"/".equals( pathPrefix ) ) {
    restClientBuilder.setPathPrefix( pathPrefix );
  }
  return restClientBuilder.build();
}

代码示例来源:origin: org.hibernate/hibernate-search-elasticsearch

private RestClient createClient(Properties properties, int maxRetryTimeoutMillis) {
  String serverUrisString = ConfigurationParseHelper.getString(
      properties,
      ElasticsearchEnvironment.SERVER_URI,
      ElasticsearchEnvironment.Defaults.SERVER_URI
  );
  ServerUris hosts = ServerUris.fromString( serverUrisString );
  String pathPrefix = ConfigurationParseHelper.getString( properties, ElasticsearchEnvironment.PATH_PREFIX, null );
  RestClientBuilder restClientBuilder = RestClient.builder( hosts.asHostsArray() )
      /*
       * Note: this timeout is currently only used on retries,
       * but should we start using the synchronous methods of RestClient,
       * it would be applied to synchronous requests too.
       * See https://github.com/elastic/elasticsearch/issues/21789#issuecomment-287399115
       */
      .setMaxRetryTimeoutMillis( maxRetryTimeoutMillis )
      .setRequestConfigCallback( (b) -> customizeRequestConfig( properties, b ) )
      .setHttpClientConfigCallback( (b) -> customizeHttpClientConfig( properties, hosts, b ) );
  if ( !StringHelper.isEmpty( pathPrefix ) && !"/".equals( pathPrefix ) ) {
    restClientBuilder.setPathPrefix( pathPrefix );
  }
  return restClientBuilder.build();
}

相关文章