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

                }

            }

        }

     

    }

  • 相关阅读:
    php中rsa加密及解密和签名及验签
    php中ssl开发的若干问题
    手机web下拉加载
    SVN:One or more files are in a conflicted state
    phpstorm安装laravel-ide-helper实现自动完成、代码提示和跟踪
    Jquery AJAX POST与GET之间的区别
    $.ajax() ,$.post(),$.get() 的用法
    PHP XML和数组互相转换
    [2017-10-25]Abp系列——集成消息队列功能(基于Rebus.Rabbitmq)
    [2017-10-26]Abp系列——DTO入参验证使用方法及经验分享
  • 原文地址:https://www.cnblogs.com/sh-0131/p/11743028.html
Copyright © 2011-2022 走看看