Vert.x httpclient - 3.5.0 throws exception "Connection was closed" intermittently

Viewed 2922

I have vert.x app which is consuming api REST over json but intermittently I am seeing exception with reason "Connection was closed". Below are my details -

  1. please share your inputs if anything wrong in the configuration. may be creating scheduler or instantiating httpclient ?
  2. on a different note is it advisable to use same http client to call more than 1 different api's on the same host and port ?

Vert.x Version: 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 Answers

My guess is that this is not related to the client. Either your server is being overloaded, or your network is unreliable. If you're consuming service which doesn't belong to you, you also may get throttled, and that's the reason you're seeing this.

In any case, you need to circumvent those problems, as the network is unreliable anyway. Make your POST requests idempotent and introduce retries.

Related