码迷,mamicode.com
首页 > Web开发 > 详细

HttpClient的连接管理器相关

时间:2019-10-26 14:54:21      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:tpc   ima   boolean   vat   run   nec   str   back   ons   

技术图片

 

 

 注意

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();
            }
        }
    }

}

 

技术图片

 

 

 定期关闭无效连接

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();
            }
        }
    }

}

设置请求参数

技术图片

 

 

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的连接管理器相关

标签:tpc   ima   boolean   vat   run   nec   str   back   ons   

原文地址:https://www.cnblogs.com/sh-0131/p/11743028.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!