HttpClient的连接管理器相关

HttpClient的连接管理器相关

 注意

public class ClientEvictExpiredConnections {

    public static void main(String[] args) throws Exception {
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        // 设置最大连接数
        cm.setMaxTotal(200);
        // 设置每个主机地址的并发数
        cm.setDefaultMaxPerRoute(20);

        new IdleConnectionEvictor(cm).start();
    }

    public static class IdleConnectionEvictor extends Thread {

        private final HttpClientConnectionManager connMgr;

        private volatile boolean shutdown;

        public IdleConnectionEvictor(HttpClientConnectionManager connMgr) {
            this.connMgr = connMgr;
        }

        @Override
        public void run() {
            try {
                while (!shutdown) {
                    synchronized (this) {
                        wait(5000);
                        // 关闭失效的连接
                        connMgr.closeExpiredConnections();
                    }
                }
            } catch (InterruptedException ex) {
                // 结束
            }
        }

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

}

HttpClient的连接管理器相关

 定期关闭无效连接

public class ClientEvictExpiredConnections {

    public static void main(String[] args) throws Exception {
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        // 设置最大连接数
        cm.setMaxTotal(200);
        // 设置每个主机地址的并发数
        cm.setDefaultMaxPerRoute(20);

        new IdleConnectionEvictor(cm).start();
    }

    public static class IdleConnectionEvictor extends Thread {

        private final HttpClientConnectionManager connMgr;

        private volatile boolean shutdown;

        public IdleConnectionEvictor(HttpClientConnectionManager connMgr) {
            this.connMgr = connMgr;
        }

        @Override
        public void run() {
            try {
                while (!shutdown) {
                    synchronized (this) {
                        wait(5000);
                        // 关闭失效的连接
                        connMgr.closeExpiredConnections();
                    }
                }
            } catch (InterruptedException ex) {
                // 结束
            }
        }

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

}

设置请求参数

HttpClient的连接管理器相关

public class ClientEvictExpiredConnections {

 

    public static void main(String[] args) throws Exception {

        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();

        // 设置最大连接数

        cm.setMaxTotal(200);

        // 设置每个主机地址的并发数

        cm.setDefaultMaxPerRoute(20);

 

        new IdleConnectionEvictor(cm).start();

    }

 

    public static class IdleConnectionEvictor extends Thread {

 

        private final HttpClientConnectionManager connMgr;

 

        private volatile boolean shutdown;

 

        public IdleConnectionEvictor(HttpClientConnectionManager connMgr) {

            this.connMgr = connMgr;

        }

 

        @Override

        public void run() {

            try {

                while (!shutdown) {

                    synchronized (this) {

                        wait(5000);

                        // 关闭失效的连接

                        connMgr.closeExpiredConnections();

                    }

                }

            } catch (InterruptedException ex) {

                // 结束

            }

        }

 

        public void shutdown() {

            shutdown = true;

            synchronized (this) {

                notifyAll();

            }

        }

    }

 

}

相关推荐