Java Low Level REST Client 官网说明-RestClient 执行请求

当RestClient被创建,就能够通过调用performRequest 或者performRequestAsync去发送请求,
performRequest是同步的,阻塞执行线程请求发送和结果读取,performRequestAsync是异步的接受一个返回监听器

这是同步的:
Request request = new Request(
    "GET",  //http method
    "/");   //server
Response response = restClient.performRequest(request);


下面是异步的

Request request = new Request(
    "GET",  //http method
    "/");   //server
Cancellable cancellable = restClient.performRequestAsync(request,
    new ResponseListener() {
        @Override
        public void onSuccess(Response response) {
            //处理成功请求
        }

        @Override
        public void onFailure(Exception exception) {
            //处理失败请求
        }
});


可以通过下面的方式增加请求参数
request.addParameter("pretty", "true");
可以通过下面的方式增加body内容
request.setEntity(new NStringEntity(
        "{\"json\":\"text\"}",
        ContentType.APPLICATION_JSON));
        
ContentType对于HttpEntity是重要的,因为它警徽被设置为同信息来帮助Elasticsearch合适的转化内容
你也能把他设置为一个字符串 默认的使用application/json, 如下

request.setJsonEntity("{\"json\":\"text\"}");

请求选项编辑类拥有在相同的应用中所有请求应该分享的部分,可以设置一个单例让所有的请求使用

private static final RequestOptions COMMON_OPTIONS;
static {
    RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
    builder.addHeader("Authorization", "Bearer " + TOKEN); 
    builder.setHttpAsyncResponseConsumerFactory(           
        new HttpAsyncResponseConsumerFactory
            .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
    COMMON_OPTIONS = builder.build();
}
比如 
增加所有请求需要的头信息
自定义返回响应的消费者
这里主要是为了授权或者在es钱有个代理设置的头信息,不需要设置Content-Type,因为客户端会自动的通过HttpEntity获取病添加到请求上

你可以设置节点选择器 它控制哪个节点将接收请求,NodeSelector.SKIP_DEDICATED_MASTERS是一个好的选择

你可以自定义响应消费者 用于去缓存异步响应,默认的消费者在jvm堆中缓存达到100m。如果响应大于100M就会失败,
比如你能降低最大size 它可能食用油的如果你在一个比如上边那个堆大小受限的环境中

一旦创建单例,你能在创建请求的时候使用它:
request.setOptions(COMMON_OPTIONS);

你也可以自定义这些选项 在每个请求上,比如增加额外的header:
RequestOptions.Builder options = COMMON_OPTIONS.toBuilder();
options.addHeader("cats", "knock things off of other things");
request.setOptions(options);
多线程异步操作编辑
客户端飞翔高兴去执行并行执行很多请求,下面的例子并行索引了很多document,在一个真是的世界中你会想使用大量的API替代


final CountDownLatch latch = new CountDownLatch(documents.length);
for (int i = 0; i < documents.length; i++) {
    Request request = new Request("PUT", "/posts/doc/" + i);
    //let's assume that the documents are stored in an HttpEntity array
    request.setEntity(documents[i]);
    restClient.performRequestAsync(
            request,
            new ResponseListener() {
                @Override
                public void onSuccess(Response response) {
                    //返回结果处理
                    latch.countDown();
                }

                @Override
                public void onFailure(Exception exception) {
                    //返回异常处理
                    latch.countDown();
                }
            }
    );
}
latch.await();

取消异步请求编辑
performRequestAsync方法返回一个Cancellable,它暴露一个public方法 cancel。这个方法能被调用去取消在执行的请求,
取消一个请求将会导致终端http请求通过隐藏的httpClient,
在服务侧 这不会自动的传递到被取消的请求的执行,这需要被api自己实现

能取消的实例的使用是可选的 并且你能安全的忽略如果你不需要使用,一个典型的使用场景是和雷士rx java
或者Kotlin’s  suspendCancellableCoRoutine框架一起使用
取消不会被需要的请求是一个好的方式去避免es中不需要的加载
The use of the Cancellable instance is optional and you can safely ignore this if you don’t need it. A typical usecase for this would be using this together with frameworks like Rx Java or the Kotlin’s suspendCancellableCoRoutine. Cancelling no longer needed requests is a good way to avoid putting unnecessary load on Elasticsearch.

Request request = new Request("GET", "/posts/_search");
Cancellable cancellable = restClient.performRequestAsync(
    request,
    new ResponseListener() {
        @Override
        public void onSuccess(Response response) {
            \\处理请求结果
        }

        @Override
        public void onFailure(Exception exception) {
            \\处理失败异常
        }
    }
);
cancellable.cancel();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值