204204import net .sf .saxon .s9api .XsltExecutable ;
205205import nu .xom .XPathException ;
206206import org .apache .http .HttpClientConnection ;
207+ import org .apache .http .HttpHost ;
207208import org .apache .http .HttpResponse ;
209+ import org .apache .http .client .HttpRequestRetryHandler ;
208210import org .apache .http .config .Registry ;
209211import org .apache .http .config .RegistryBuilder ;
210212import org .apache .http .conn .ConnectionKeepAliveStrategy ;
214216import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
215217import org .apache .http .impl .conn .PoolingHttpClientConnectionManager ;
216218import org .apache .http .protocol .HttpContext ;
219+ import org .apache .http .protocol .HttpCoreContext ;
217220import org .apache .jena .query .DatasetFactory ;
218221import org .apache .jena .rdf .model .ModelFactory ;
219222import org .apache .jena .rdf .model .ResIterator ;
230233import org .glassfish .hk2 .utilities .binding .AbstractBinder ;
231234import org .glassfish .jersey .client .ClientConfig ;
232235import org .glassfish .jersey .apache .connector .ApacheClientProperties ;
233- import org .glassfish .jersey .apache .connector .ApacheConnectionClosingStrategy ;
234236import org .glassfish .jersey .apache .connector .ApacheConnectorProvider ;
235237import org .glassfish .jersey .client .ClientProperties ;
236238import org .glassfish .jersey .client .RequestEntityProcessing ;
@@ -1384,7 +1386,8 @@ public void releaseConnection(final HttpClientConnection managedConn, final Obje
13841386 };
13851387 if (maxConnPerRoute != null ) conman .setDefaultMaxPerRoute (maxConnPerRoute );
13861388 if (maxTotalConn != null ) conman .setMaxTotal (maxTotalConn );
1387-
1389+ int maxRetryCount = 3 ;
1390+
13881391 ClientConfig config = new ClientConfig ();
13891392 config .connectorProvider (new ApacheConnectorProvider ());
13901393 config .register (MultiPartFeature .class );
@@ -1396,6 +1399,23 @@ public void releaseConnection(final HttpClientConnection managedConn, final Obje
13961399 config .property (ClientProperties .FOLLOW_REDIRECTS , true );
13971400 config .property (ClientProperties .REQUEST_ENTITY_PROCESSING , RequestEntityProcessing .BUFFERED ); // https://stackoverflow.com/questions/42139436/jersey-client-throws-cannot-retry-request-with-a-non-repeatable-request-entity
13981401 config .property (ApacheClientProperties .CONNECTION_MANAGER , conman );
1402+ config .property (ApacheClientProperties .RETRY_HANDLER , (HttpRequestRetryHandler ) (IOException ex , int executionCount , HttpContext context ) ->
1403+ {
1404+ // Extract the HTTP host from the context
1405+ HttpHost targetHost = (HttpHost ) context .getAttribute (HttpCoreContext .HTTP_TARGET_HOST );
1406+ String serverName = targetHost != null ? targetHost .getHostName () : "Unknown" ;
1407+
1408+ if (executionCount > maxRetryCount ) {
1409+ if (log .isWarnEnabled ()) log .warn ("Maximum tries reached for client HTTP pool to server '{}'" , serverName );
1410+ return false ;
1411+ }
1412+ if (ex instanceof org .apache .http .NoHttpResponseException ) {
1413+ if (log .isWarnEnabled ()) log .warn ("No response from server '{}' on {} call" , serverName , executionCount );
1414+ return true ;
1415+ }
1416+ return false ;
1417+ });
1418+
13991419 //config.property(ApacheClientProperties.CONNECTION_CLOSING_STRATEGY, new ApacheConnectionClosingStrategy.GracefulClosingStrategy());
14001420 if (keepAliveStrategy != null ) config .property (ApacheClientProperties .KEEPALIVE_STRATEGY , keepAliveStrategy );
14011421
@@ -1461,6 +1481,7 @@ public void releaseConnection(final HttpClientConnection managedConn, final Obje
14611481 };
14621482 if (maxConnPerRoute != null ) conman .setDefaultMaxPerRoute (maxConnPerRoute );
14631483 if (maxTotalConn != null ) conman .setMaxTotal (maxTotalConn );
1484+ int maxRetryCount = 3 ;
14641485
14651486 ClientConfig config = new ClientConfig ();
14661487 config .connectorProvider (new ApacheConnectorProvider ());
@@ -1473,7 +1494,23 @@ public void releaseConnection(final HttpClientConnection managedConn, final Obje
14731494 config .property (ClientProperties .FOLLOW_REDIRECTS , true );
14741495 config .property (ClientProperties .REQUEST_ENTITY_PROCESSING , RequestEntityProcessing .BUFFERED ); // https://stackoverflow.com/questions/42139436/jersey-client-throws-cannot-retry-request-with-a-non-repeatable-request-entity
14751496 config .property (ApacheClientProperties .CONNECTION_MANAGER , conman );
1497+ config .property (ApacheClientProperties .RETRY_HANDLER , (HttpRequestRetryHandler ) (IOException ex , int executionCount , HttpContext context ) ->
1498+ {
1499+ // Extract the HTTP host from the context
1500+ HttpHost targetHost = (HttpHost ) context .getAttribute (HttpCoreContext .HTTP_TARGET_HOST );
1501+ String serverName = targetHost != null ? targetHost .getHostName () : "Unknown" ;
14761502
1503+ if (executionCount > maxRetryCount ) {
1504+ if (log .isWarnEnabled ()) log .warn ("Maximum tries reached for client HTTP pool to server '{}'" , serverName );
1505+ return false ;
1506+ }
1507+ if (ex instanceof org .apache .http .NoHttpResponseException ) {
1508+ if (log .isWarnEnabled ()) log .warn ("No response from server '{}' on {} call" , serverName , executionCount );
1509+ return true ;
1510+ }
1511+ return false ;
1512+ });
1513+
14771514 return ClientBuilder .newBuilder ().
14781515 withConfig (config ).
14791516 sslContext (ctx ).
0 commit comments