Skip to content

Commit eb62fd1

Browse files
committed
Create ScheduledExecutorService in both createClient and createNoCertClient
1 parent 938da41 commit eb62fd1

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

src/main/java/com/atomgraph/linkeddatahub/Application.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,6 @@
171171
import com.atomgraph.spinrdf.vocabulary.SP;
172172
import com.github.jsonldjava.core.DocumentLoader;
173173
import com.github.jsonldjava.core.JsonLdOptions;
174-
import jakarta.inject.Inject;
175-
import jakarta.inject.Named;
176174
import java.io.FileOutputStream;
177175
import java.io.UnsupportedEncodingException;
178176
import java.nio.charset.StandardCharsets;
@@ -238,7 +236,6 @@
238236
import org.glassfish.jersey.client.RequestEntityProcessing;
239237
import org.glassfish.jersey.media.multipart.MultiPartFeature;
240238
import org.glassfish.jersey.process.internal.RequestScoped;
241-
import org.glassfish.jersey.server.BackgroundScheduler;
242239
import org.glassfish.jersey.server.ResourceConfig;
243240
import org.glassfish.jersey.server.filter.HttpMethodOverrideFilter;
244241

@@ -254,7 +251,6 @@ public class Application extends ResourceConfig
254251

255252
private static final Logger log = LoggerFactory.getLogger(Application.class);
256253

257-
private @Inject @BackgroundScheduler @Named("jersey-background-task-scheduler") ScheduledExecutorService idleConnectionMonitor;
258254
private final ExecutorService importThreadPool;
259255
private final ServletConfig servletConfig;
260256
private final EventBus eventBus = new EventBus();
@@ -1335,7 +1331,7 @@ public void submitImport(RDFImport rdfImport, com.atomgraph.linkeddatahub.apps.m
13351331
* @throws UnrecoverableKeyException key loading error
13361332
* @throws KeyManagementException key loading error
13371333
*/
1338-
public Client createClient(KeyStore keyStore, String keyStorePassword, KeyStore trustStore, Integer maxConnPerRoute, Integer maxTotalConn, ConnectionKeepAliveStrategy keepAliveStrategy, boolean buffered) throws NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException, KeyManagementException
1334+
public static Client createClient(KeyStore keyStore, String keyStorePassword, KeyStore trustStore, Integer maxConnPerRoute, Integer maxTotalConn, ConnectionKeepAliveStrategy keepAliveStrategy, boolean buffered) throws NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException, KeyManagementException
13391335
{
13401336
if (keyStore == null) throw new IllegalArgumentException("KeyStore cannot be null");
13411337
if (keyStorePassword == null) throw new IllegalArgumentException("KeyStore password string cannot be null");
@@ -1389,8 +1385,23 @@ public void releaseConnection(final HttpClientConnection managedConn, final Obje
13891385
if (maxConnPerRoute != null) conman.setDefaultMaxPerRoute(maxConnPerRoute);
13901386
if (maxTotalConn != null) conman.setMaxTotal(maxTotalConn);
13911387
conman.setValidateAfterInactivity(5000); // check connections idle for more than Varnish's idle_timeout which is 5s
1392-
//conman.closeIdleConnections(5, TimeUnit.SECONDS); // Match the Varnish idle timeout
13931388

1389+
Integer idleConnTimeout = 5000;
1390+
// create monitor thread that evicts idle connections: https://hc.apache.org/httpcomponents-client-4.5.x/current/tutorial/html/connmgmt.html#d5e418
1391+
ScheduledExecutorService idleConnectionMonitor = Executors.newSingleThreadScheduledExecutor();
1392+
idleConnectionMonitor.scheduleAtFixedRate(() ->
1393+
{
1394+
try
1395+
{
1396+
if (log.isDebugEnabled()) log.debug("Evicting idle HTTP connections (every {} ms)", idleConnTimeout);
1397+
conman.closeIdleConnections(idleConnTimeout, TimeUnit.MILLISECONDS);
1398+
}
1399+
catch (Exception ex)
1400+
{
1401+
if (log.isErrorEnabled()) log.error("Error closing idle connections: {}", ex);
1402+
}
1403+
}, 0, idleConnTimeout, java.util.concurrent.TimeUnit.MILLISECONDS);
1404+
13941405
ClientConfig config = new ClientConfig();
13951406
config.connectorProvider(new ApacheConnectorProvider());
13961407
config.register(MultiPartFeature.class);
@@ -1421,7 +1432,7 @@ public void releaseConnection(final HttpClientConnection managedConn, final Obje
14211432
* @param maxTotalConn max total connections
14221433
* @return client instance
14231434
*/
1424-
public Client createNoCertClient(KeyStore trustStore, Integer maxConnPerRoute, Integer maxTotalConn)
1435+
public static Client createNoCertClient(KeyStore trustStore, Integer maxConnPerRoute, Integer maxTotalConn)
14251436
{
14261437
try
14271438
{
@@ -1472,6 +1483,7 @@ public void releaseConnection(final HttpClientConnection managedConn, final Obje
14721483

14731484
Integer idleConnTimeout = 5000;
14741485
// create monitor thread that evicts idle connections: https://hc.apache.org/httpcomponents-client-4.5.x/current/tutorial/html/connmgmt.html#d5e418
1486+
ScheduledExecutorService idleConnectionMonitor = Executors.newSingleThreadScheduledExecutor();
14751487
idleConnectionMonitor.scheduleAtFixedRate(() ->
14761488
{
14771489
try
@@ -1496,7 +1508,7 @@ public void releaseConnection(final HttpClientConnection managedConn, final Obje
14961508
config.property(ClientProperties.FOLLOW_REDIRECTS, true);
14971509
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
14981510
config.property(ApacheClientProperties.CONNECTION_MANAGER, conman);
1499-
1511+
15001512
return ClientBuilder.newBuilder().
15011513
withConfig(config).
15021514
sslContext(ctx).

0 commit comments

Comments
 (0)