httpClient入门到精通-------连接池的关闭

来源:互联网 发布:赤峰市用友软件销售 编辑:程序博客网 时间:2024/05/29 19:34

官方给出的关闭方法

/* * ==================================================================== * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements.  See the NOTICE file * distributed with this work for additional information * regarding copyright ownership.  The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License.  You may obtain a copy of the License at * *   http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied.  See the License for the * specific language governing permissions and limitations * under the License. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation.  For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */package cn.itcast.httpclient;import org.apache.http.conn.HttpClientConnectionManager;import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;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();            }        }    }}
阅读全文
0 0
原创粉丝点击