user29495070
user29495070

Reputation: 1

Jersey client connection reuse with chunked gzip response

I am trying to use a Jersey (3.x) client with ApacheConnector but I observed that in case a chunked gzip response is received. the connection gets closed as soon as the response is read. The connection is not reused even after configuring a connection pool, keep-alive strategy and reuse strategy.

The request seems to be aborted in the following code as it is chunked and because the connection was not yet released, it is shutdown.

https://github.com/eclipse-ee4j/jersey/blob/b128e9c9e649e96488a9c1d367e4cbae89a9d1d4/connectors/apache-connector/src/main/java/org/glassfish/jersey/apache/connector/ApacheConnectionClosingStrategy.java#L75 request.abort();

However this seems to work correctly with non-gzipped chunked responses. Is Jersey not expected to work with gzip chunked responses? Or can I do something to make it work correctly here?

Reference Code:

PoolingHttpClientConnectionManager connectionManager2 = new PoolingHttpClientConnectionManager();
connectionManager2.setMaxTotal(100); // Maximum total connections
connectionManager2.setDefaultMaxPerRoute(100); // Maximum connections per route

final ClientConfig clientConfig = new ClientConfig();
clientConfig.connectorProvider(new ApacheConnectorProvider());
clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager2);
clientConfig.property(ApacheClientProperties.KEEPALIVE_STRATEGY,
    (ConnectionKeepAliveStrategy) (response, context) -> 60000);
clientConfig.property(ApacheClientProperties.REUSE_STRATEGY,
    (ConnectionReuseStrategy) (response, context) -> true);

// Build the Jersey client
JERSEY_CLIENT = ClientBuilder.newClient(clientConfig);

final var request = JERSEY_CLIENT.target(getRequestUri()).request(MediaType.APPLICATION_JSON);
var response = request.post(Entity.entity(JsonUtil.toJsonString(requestBody), MediaType.APPLICATION_JSON));
var responseEntity = response.readEntity(String.class); // request abort and connection shutdown happens at this call.

I tried reading entity with InputStream.class instead of String.class as well but that also seems to produce the same result even if I close that InputStream. Also, seeing this issue with both Jersey 3 and Jersey 2 if that helps.

Upvotes: 0

Views: 26

Answers (1)

jan.supol
jan.supol

Reputation: 2805

The ApacheClosingStrategy is there for the customers to override in some non-standard use cases they can have. Simply use

client.property(ApacheClientProperties.CONNECTION_CLOSING_STRATEGY, new MyApacheClosingStrategy())

and implement the strategy as it suits you.

Upvotes: 0

Related Questions