Jorge Tapia created OLINGO-1621:
-----------------------------------

             Summary: can't reuse HTTP connections
                 Key: OLINGO-1621
                 URL: https://issues.apache.org/jira/browse/OLINGO-1621
             Project: Olingo
          Issue Type: Bug
          Components: odata4-client
    Affects Versions: (Java) V4 5.0.1
            Reporter: Jorge Tapia


We want to reuse HTTP connections but Olingo V4 library is closing the 
connection (and discarding it) every time we have a 200 OK response from the 
server.

Here you have a test code to demonstrate the issue:
{code:java}
package com.jtaphie;

import java.io.InputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.olingo.client.api.ODataClient;
import 
org.apache.olingo.client.api.communication.request.retrieve.ODataEntityRequest;
import 
org.apache.olingo.client.api.communication.response.ODataRetrieveResponse;
import org.apache.olingo.client.api.domain.ClientEntity;
import org.apache.olingo.client.api.http.HttpClientFactory;
import org.apache.olingo.client.api.http.NoContentException;
import org.apache.olingo.client.core.ODataClientFactory;
import org.apache.olingo.commons.api.ex.ODataRuntimeException;
import org.apache.olingo.commons.api.http.HttpMethod;

public class ReuseHttpConnectionTest {

  public static void main(String[] args) throws Exception {
    ReusableHttpClientFactory httpClientFactory = new 
ReusableHttpClientFactory();
    ODataClient client = ODataClientFactory.getClient();
    client.getConfiguration().setHttpClientFactory(httpClientFactory);

    ODataEntityRequest<ClientEntity> request =
        client
            .getRetrieveRequestFactory()
            .getEntityRequest(
                new URI(
                    
"https://services.odata.org/V4/(S(ckjlngqm5lub5c5osrvdfvbf))/TripPinServiceRW/Photos(1)"));

    ODataRetrieveResponse<ClientEntity> response = null;
    try {
      response = request.execute();
    } catch (ODataRuntimeException e) {
      // HTTP 404
    }

    if (response != null) {
      try (InputStream is = response.getRawResponse()) {
        System.out.println(IOUtils.toString(is, StandardCharsets.UTF_8));
      } catch (NoContentException e) {
        // HTTP 204
      } finally {
        response.close();
      }
    }

    System.out.print(httpClientFactory.connectionManager.getTotalStats());
  }

  private static class ReusableHttpClientFactory implements HttpClientFactory {
    private final PoolingHttpClientConnectionManager connectionManager;
    private final HttpClient httpClient;

    private ReusableHttpClientFactory() {
      connectionManager = new PoolingHttpClientConnectionManager();
      httpClient = 
HttpClients.custom().setConnectionManager(connectionManager).build();
    }

    @Override
    public HttpClient create(HttpMethod method, URI uri) {
      return httpClient;
    }

    @Override
    public void close(HttpClient httpClient) {
      // do nothing as we want to reuse the same http client instance
    }
  }
}
{code}

The output of the test should be:

{noformat}
[leased: 0; pending: 0; available: 1; max: 20]
{noformat}

But it is:

{noformat}
[leased: 0; pending: 0; available: 0; max: 20]
{noformat}

The reason is that the library closes the response instead of consuming it.

Is it a bug or is there any other way to reuse the connections?

Thanks,

Jorge



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to