发送请求

一旦创建了 RestClient,就可以通过调用其中一个performRequestperformRequestAsync方法来发送请求。 performRequest 方法是同步的,并直接返回Response,这意味着客户端将阻塞并等待返回的响应。 performRequestAsync 返回 void,并接受一个额外的ResponseListener作为参数,这意味着它们是异步执行的。 提供的监听器将在请求完成或失败时通知。

  1. Response response = restClient.performRequest("GET", "/"); //最简单的发送一个请求
  1. Map<String, String> params = Collections.singletonMap("pretty", "true");
  2. Response response = restClient.performRequest("GET", "/", params); //发送一个带参数的请求
  1. Map<String, String> params = Collections.emptyMap();
  2. String jsonString = "{" +
  3. "\"user\":\"kimchy\"," +
  4. "\"postDate\":\"2013-01-30\"," +
  5. "\"message\":\"trying out Elasticsearch\"" +
  6. "}";
  7. HttpEntity entity = new NStringEntity(jsonString, ContentType.APPLICATION_JSON);// org.apache.http.HttpEntity 为了让Elasticsearch 能够解析,需要设置ContentType。
  8. Response response = restClient.performRequest("PUT", "/posts/doc/1", params, entity);
  1. Map<String, String> params = Collections.emptyMap();
  2. HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory consumerFactory =
  3. new HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory(30 * 1024 * 1024); //[ org.apache.http.nio.protocol.HttpAsyncResponseConsumer](http://hc.apache.org/httpcomponents-core-ga/httpcore-nio/apidocs/org/apache/http/nio/protocol/HttpAsyncResponseConsumer.html) ,
  4. Response response = restClient.performRequest("GET", "/posts/_search", params, null, consumerFactory);
  1. ResponseListener responseListener = new ResponseListener() {
  2. @Override
  3. public void onSuccess(Response response) {
  4. // 请求成功回调
  5. }
  6. @Override
  7. public void onFailure(Exception exception) {
  8. //请求失败时回调
  9. }
  10. };
  11. restClient.performRequestAsync("GET", "/", responseListener); //发送异步请求
  1. Map<String, String> params = Collections.singletonMap("pretty", "true");
  2. restClient.performRequestAsync("GET", "/", params, responseListener); // 发送带参数的异步请求
  1. String jsonString = "{" +
  2. "\"user\":\"kimchy\"," +
  3. "\"postDate\":\"2013-01-30\"," +
  4. "\"message\":\"trying out Elasticsearch\"" +
  5. "}";
  6. HttpEntity entity = new NStringEntity(jsonString, ContentType.APPLICATION_JSON);
  7. restClient.performRequestAsync("PUT", "/posts/doc/1", params, entity, responseListener);
  1. HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory consumerFactory =
  2. new HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory(30 * 1024 * 1024);
  3. restClient.performRequestAsync("GET", "/posts/_search", params, null, consumerFactory, responseListener);

以下是如何发送异步请求的基本示例:

  1. final CountDownLatch latch = new CountDownLatch(documents.length);
  2. for (int i = 0; i < documents.length; i++) {
  3. restClient.performRequestAsync(
  4. "PUT",
  5. "/posts/doc/" + i,
  6. Collections.<String, String>emptyMap(),
  7. //let's assume that the documents are stored in an HttpEntity array
  8. documents[i],
  9. new ResponseListener() {
  10. @Override
  11. public void onSuccess(Response response) {
  12. latch.countDown();//处理返回响应
  13. }
  14. @Override
  15. public void onFailure(Exception exception) {
  16. latch.countDown();//处理失败响应,exception 里带错误码
  17. }
  18. }
  19. );
  20. }
  21. latch.await();

上面列出的每一种方法都支持通过Header varargs参数和请求一起headers,如下例所示:

  1. Response response = restClient.performRequest("GET", "/", new BasicHeader("header", "value"));
  1. Header[] headers = {
  2. new BasicHeader("header1", "value1"),
  3. new BasicHeader("header2", "value2")
  4. };
  5. restClient.performRequestAsync("GET", "/", responseListener, headers);