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

                }

            }

        }

     

    }

  • 相关阅读:
    MySQL 安装和配置
    其它 Google Chrome 已停止工作
    VUE 父组件和子组件相互传值 组件之间的数据传递
    SpringBlade 源码 个性化修改 添加字典时 自动填充字典值
    MyBatis-Plus SpringBlade 生成代码时 啥内容都没有 只有目录
    Oracle IIS部署报错
    pycharm连接sqlite后打开db文件不显示表的问题
    Hbase集群搭建以及启动(单点启动,群起)
    Flume的put和take事务
    稀疏数组学习
  • 原文地址:https://www.cnblogs.com/sh-0131/p/11743028.html
Copyright © 2011-2022 走看看