Talk about jest’s IdleConnectionReaper

  jest

Order

This article mainly studies jest’s IdleConnectionReaper

IdleConnectionReaper

jest-common-6.3.1-sources.jar! /io/searchbox/client/config/idle/IdleConnectionReaper.java

public class IdleConnectionReaper extends AbstractScheduledService {

    final static Logger logger = LoggerFactory.getLogger(IdleConnectionReaper.class);

    private final ReapableConnectionManager reapableConnectionManager;
    private final ClientConfig clientConfig;

    public IdleConnectionReaper(ClientConfig clientConfig, ReapableConnectionManager reapableConnectionManager) {
        this.reapableConnectionManager = reapableConnectionManager;
        this.clientConfig = clientConfig;
    }

    @Override
    protected void runOneIteration() throws Exception {
        logger.debug("closing idle connections...");
        reapableConnectionManager.closeIdleConnections(clientConfig.getMaxConnectionIdleTime(),
                                                       clientConfig.getMaxConnectionIdleTimeDurationTimeUnit());
    }

    @Override
    protected Scheduler scheduler() {
        return Scheduler.newFixedDelaySchedule(0l,
                                               clientConfig.getMaxConnectionIdleTime(),
                                               clientConfig.getMaxConnectionIdleTimeDurationTimeUnit());
    }

    @Override
    protected ScheduledExecutorService executor() {
        final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(
            new ThreadFactoryBuilder()
                .setDaemon(true)
                .setNameFormat(serviceName())
                .build());
        // Add a listener to shutdown the executor after the service is stopped.  This ensures that the
        // JVM shutdown will not be prevented from exiting after this service has stopped or failed.
        // Technically this listener is added after start() was called so it is a little gross, but it
        // is called within doStart() so we know that the service cannot terminate or fail concurrently
        // with adding this listener so it is impossible to miss an event that we are interested in.
        addListener(new Listener() {
            @Override public void terminated(State from) {
                executor.shutdown();
            }
            @Override public void failed(State from, Throwable failure) {
                executor.shutdown();
            }}, MoreExecutors.directExecutor());
        return executor;
    }
}
  • IdleConnectionReaper inherits AbstractScheduledService, whose constructor receives clientConfig and reapableConnectionManager; ; Its runOneIteration executed reapableconnectionmanager.closedconnections; Its scheduler method creates a fixedDelay Scheduler; ; Its executor method creates SingleThreadScheduledExecutor

ReapableConnectionManager

jest-common-6.3.1-sources.jar! /io/searchbox/client/config/idle/ReapableConnectionManager.java

public interface ReapableConnectionManager {
    void closeIdleConnections(long idleTimeout, TimeUnit unit);
}
  • The ReapableConnectionManager interface defines the closeIdleConnections method

HttpReapableConnectionManager

jest-6.3.1-sources.jar! /io/searchbox/client/config/idle/HttpReapableConnectionManager.java

public class HttpReapableConnectionManager implements ReapableConnectionManager {
    private final HttpClientConnectionManager connectionManager;
    private final NHttpClientConnectionManager nConnectionManager;

    public HttpReapableConnectionManager(HttpClientConnectionManager connectionManager, NHttpClientConnectionManager nConnectionManager) {
        if(connectionManager == null || nConnectionManager == null) throw new IllegalArgumentException();

        this.connectionManager = connectionManager;
        this.nConnectionManager = nConnectionManager;
    }

    @Override
    public void closeIdleConnections(long idleTimeout, TimeUnit unit) {
        connectionManager.closeIdleConnections(idleTimeout, unit);
        nConnectionManager.closeIdleConnections(idleTimeout, unit);
    }
}
  • HttpProcessableConnectionManager implements the ReapableConnectionManager interface; Its constructor requires the input of connectionManager and ConnectionManager, which cannot be null; at the same time; The closeIdleConnections method implements connectionmanager.closeidleconnections and ncconnectionmanager.closeidleconnections respectively.

Summary

  • IdleConnectionReaper inherits AbstractScheduledService, whose constructor receives clientConfig and reapableConnectionManager; ; Its runOneIteration executed reapableconnectionmanager.closedconnections; Its scheduler method creates a fixedDelay Scheduler; ; Its executor method creates SingleThreadScheduledExecutor
  • The ReapableConnectionManager interface defines the closeIdleConnections method
  • HttpProcessableConnectionManager implements the ReapableConnectionManager interface; Its constructor requires the input of connectionManager and ConnectionManager, which cannot be null; at the same time; The closeIdleConnections method implements connectionmanager.closeidleconnections and ncconnectionmanager.closeidleconnections respectively.

doc