有没有办法在“PoolingHttpClientConnectionManager”每次创建连接时调用方法?

时间:2018-05-20 08:39:01

标签: java connection-pooling apache-httpclient-4.x apache-commons-httpclient

我正在使用Apache HTTP客户端来实现HTTP连接池,并且每次连接池创建连接时都需要调用方法。我检查了它是否允许我们覆盖方法这会产生连接,但却没有。有没有办法实现这个目标?

    import org.apache.http.*;
    import org.apache.http.conn.ConnectionKeepAliveStrategy;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.impl.client.TargetAuthenticationStrategy;
    import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
    import org.apache.http.protocol.HttpContext;
    import org.apache.log4j.LogManager;
    import org.apache.log4j.Logger;

    import java.util.concurrent.TimeUnit;

    public class PoolingConnectionFactory {

    public PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();

     public CloseableHttpClient CreateHttpClient(PoolingHttpClientConnectionManager connManager) {

            CloseableHttpClient client = HttpClients.custom()
                    .setTargetAuthenticationStrategy(new AuthendictionSessionStrategy())
                    .setKeepAliveStrategy(strategy)
                    .setConnectionManager(connManager)
                    .build();

            return client;
        }

        public CloseableHttpClient getConnection(PoolingHttpClientConnectionManager connMan) {

            connMan.closeExpiredConnections(); //Close the expired http connections which are expired
            connMan.closeIdleConnections(50, TimeUnit.SECONDS);  //if http connection is idle for 50s then it will close
            CloseableHttpClient client = CreateHttpClient(connMan);
            return client;
        }
    }

1 个答案:

答案 0 :(得分:0)

有多个插件点。可以在套接字工厂级别或连接操作员级别注入自定义代码。

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(
        new DefaultHttpClientConnectionOperator(
                RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("http", new PlainConnectionSocketFactory() {

                        @Override
                        public Socket connectSocket(
                                int connectTimeout,
                                Socket socket,
                                HttpHost host,
                                InetSocketAddress remoteAddress,
                                InetSocketAddress localAddress,
                                HttpContext context) throws IOException {
                            return super.connectSocket(connectTimeout, socket, host, remoteAddress, localAddress, context);
                        }
                    })
                    .register("https", new SSLConnectionSocketFactory(SSLContexts.createSystemDefault()) {

                        @Override
                        public Socket connectSocket(
                                int connectTimeout,
                                Socket socket,
                                HttpHost host,
                                InetSocketAddress remoteAddress,
                                InetSocketAddress localAddress,
                                HttpContext context) throws IOException {
                            return super.connectSocket(connectTimeout, socket, host, remoteAddress, localAddress, context);
                        }
                    })
                    .build(),
                DefaultSchemePortResolver.INSTANCE,
                SystemDefaultDnsResolver.INSTANCE) {

            @Override
            public void connect(
                    ManagedHttpClientConnection conn,
                    HttpHost host,
                    InetSocketAddress localAddress,
                    int connectTimeout,
                    SocketConfig socketConfig,
                    HttpContext context) throws IOException {
                super.connect(conn, host, localAddress, connectTimeout, socketConfig, context);
            }
        },
        ManagedHttpClientConnectionFactory.INSTANCE,
        0L, TimeUnit.MILLISECONDS);
CloseableHttpClient client = HttpClients.custom()
        .setConnectionManager(cm)
        .build();