ApacheHTTPClient Pooling but each request has some statefullness

11 Views Asked by At
  • I am using a connector where i can only configure httpClient
  • The connector generates the token based on some external call
  • But i need to override this behavior as i already have the token in the request.
  • The connector is written in such a way that i can only customize HTTPClient

One solution can be to generate new HTTPClient everytime with an interceptor to inject token

private CloseableHttpClient createHttpClient(String accessToken){

return  HttpClientBuilder.create()
        .addInterceptorLast(
                (HttpRequestInterceptor) (request, context) -> {
                  request.addHeader(HttpHeaders.AUTHORIZATION, accessToken);
                }).build();
    }

But i also want to leverage pooling. Is the implementation below fine or there can be some pitfalls ?

    private static HttpClientConnectionManager poolingConnManager
              = new PoolingHttpClientConnectionManager();

  private CloseableHttpClient createHttpClient(String accessToken){
   
    return  HttpClientBuilder.create()
            .setConnectionManager(poolingConnManager)
            .setConnectionManagerShared(true)
            .addInterceptorLast(
                    (HttpRequestInterceptor) (request, context) -> {
                      request.addHeader(HttpHeaders.AUTHORIZATION, accessToken);
                    }).build();
     }
0

There are 0 best solutions below