Vert.x httpclient-3.5.0间歇性地引发异常“连接已关闭”

时间:2018-08-27 04:24:49

标签: json rest exception vert.x keep-alive

我有vert.x应用程序,它在json上使用api REST,但是间歇性地我看到异常,原因是“连接已关闭”。以下是我的详细信息-

  1. 如果配置有任何错误,请共享您的输入。可能正在创建调度程序或实例化httpclient?
  2. 另外,建议使用同一http客户端在同一主机和端口上调用多个1个以上的不同api吗?

Vert.x版本:3.5.0

import io.vertx.core.http.HttpClient;

private static Scheduler scheduler = 
Schedulers.from(Executors.newFixedThreadPool(8));

// http client instantiated at the time of verticle startup
HttpClient httpclient = vertx.createHttpClient(getHttpClientOptions());

public static HttpClientOptions getHttpClientOptions() {
    return new HttpClientOptions()  
            .setKeepAlive(true)
            .setMaxPoolSize(100)
            .setPipelining(true)
            .setDefaultHost(xxxx.xxxx.com)
            .setDefaultPort(8084)
            .setSsl(true);
}

 // invoke api call
public static Single<Response> invokePOSTServiceAsync(String  reqBodyStr, String endpointURI) throws Exception {
        try{
        return Single.create((SingleEmitter<Response> emitter) -> {
            HttpClientRequest request = httpClient.post(endpointURI);
      request.putHeader("Content-type","application/json")  
            request.exceptionHandler(error -> {
                        LOG.error("ExceptionHandler "+error.getMessage());
                        emitter.onError(new Throwable(" Failure"));
                    })
                    .handler(response -> {
                        int statusCode = response.statusCode();
                        if (statusCode == 200) {
                            response.bodyHandler(body -> {
                                StringBuilder responseData = new StringBuilder();
                                responseData.append(body);
                                emitter.onSuccess(new Response(statusCode,responseData.toString(),"","",null));
                            });
                        } else {
                            emitter.onError(new Throwable(" Failure"));
                        }
                    })
                    .putHeader(HttpHeaders.CONTENT_LENGTH, reqBodyStr.length() + "")
                    .setTimeout(6000)
                    .write(reqBodyStr)
                    .end();
                }).subscribeOn(scheduler); 
        }catch(Exception exe){
            exe.printStackTrace();
            return null;
        }
    }   

1 个答案:

答案 0 :(得分:0)

我的猜测是,这与客户无关。您的服务器超载,或者网络不可靠。如果您使用的服务不属于您,那么您也可能会受到限制,这就是您看到这种情况的原因。

无论如何,您都需要规避这些问题,因为网络仍然是不可靠的。使您的POST请求成为幂等并引入重试。