org.aevans.goat.net.IdleConnectionEvictor.java Source code

Java tutorial

Introduction

Here is the source code for org.aevans.goat.net.IdleConnectionEvictor.java

Source

package org.aevans.goat.net;

import org.apache.http.nio.conn.NHttpClientConnectionManager;
import java.util.concurrent.TimeUnit;

/**
 * Idle Conenction Evictor for Apache HTTP Pools
 * @author Andrew Evans
 * Copyright 2016
 * License : Free BSD
 */
public class IdleConnectionEvictor extends Thread {

    private final NHttpClientConnectionManager connMgr;

    private volatile boolean shutdown;

    public IdleConnectionEvictor(NHttpClientConnectionManager connMgr) {
        super();
        this.connMgr = connMgr;
    }

    @Override
    public void run() {
        try {
            while (!shutdown) {
                synchronized (this) {
                    wait(5000);
                    // Close expired connections
                    connMgr.closeExpiredConnections();
                    // Optionally, close connections
                    // that have been idle longer than 5 sec
                    connMgr.closeIdleConnections(5, TimeUnit.SECONDS);
                }
            }
        } catch (InterruptedException ex) {

        }
    }

    public void shutdown() {
        shutdown = true;
        synchronized (this) {
            notifyAll();
        }
    }

}