Reputation: 1
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.
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
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