zoukankan      html  css  js  c++  java
  • 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();
                }
            }
        }
    
    }

     定期关闭无效连接

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

                }

            }

        }

     

    }

  • 相关阅读:
    几种开源工作流引擎的简单比较(转)
    ExecuteScalar
    机房重构---我们“重构”出了什么?
    薏米红豆粥功效及做法介绍
    Mean Shift具体介绍
    linux fork函数浅析
    html的下拉框的几个基本使用方法
    Readprocessmemory使用方法
    配置Log4j(非常具体)
    【Linux】linux经常使用基本命令
  • 原文地址:https://www.cnblogs.com/sh-0131/p/11743028.html
Copyright © 2011-2022 走看看