httpclient连接池在ES Restful API请求中的应用
package com.wm.utils; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; /** * 1.功能:http连接池 */ public class HttpPoolManager { public static PoolingHttpClientConnectionManager clientConnectionManager = null; private int maxTotal = 50; private int defaultMaxPerRoute = 25; private HttpPoolManager(int maxTotal, int defaultMaxPerRoute) { this.maxTotal = maxTotal; this.defaultMaxPerRoute = defaultMaxPerRoute; clientConnectionManager.setMaxTotal(maxTotal); clientConnectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute); } private HttpPoolManager() { clientConnectionManager.setMaxTotal(maxTotal); clientConnectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute); } private static HttpPoolManager poolManager = null; /* * 获取实例1 */ public synchronized static HttpPoolManager getInstance() { if (poolManager == null) { clientConnectionManager = new PoolingHttpClientConnectionManager(); poolManager = new HttpPoolManager(); } return poolManager; } /* * 获取实例1 */ public synchronized static HttpPoolManager getInstance(int maxTotal, int defaultMaxPerRoute) { if (poolManager == null) { poolManager = new HttpPoolManager(maxTotal, defaultMaxPerRoute); } return poolManager; } /* * 获取CloseableHttpClient */ public static CloseableHttpClient getHttpClient() { if (clientConnectionManager == null) { clientConnectionManager = new PoolingHttpClientConnectionManager(); getInstance(); } return HttpClients.custom().setConnectionManager(clientConnectionManager).build(); } }
为什么使用httpclient连接池?
最近在使用ES做全文搜索,使用的restful api操作,最初没有使用httpclient连接池,这样导致每次请求都重建一个httpclient实例,es服务器报如下异常:
[2017-07-19T13:44:14,768][WARN ][o.e.h.n.Netty4HttpServerTransport] [gxKNWf3] caught exception while handling client http traffic, closing connection [id: 0xe0578a52, L:/127.0.0.1:9200 - R:/127.0.0.1:49956] java.io.IOException: 远程主机强迫关闭了一个现有的连接。 at sun.nio.ch.SocketDispatcher.read0(Native Method) ~[?:?] at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43) ~[?:?] at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223) ~[?:?] at sun.nio.ch.IOUtil.read(IOUtil.java:197) ~[?:?] at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380) ~[?:?] at io.netty.buffer.PooledHeapByteBuf.setBytes(PooledHeapByteBuf.java:261) ~[netty-buffer-4.1.11.Final.jar:4.1.11.Final] at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1100) ~[netty-buffer-4.1.11.Final.jar:4.1.11.Final] at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:372) ~[netty-transport-4.1.11.Final.jar:4.1.11.Final] at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:123) [netty-transport-4.1.11.Final.jar:4.1.11.Final] at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:644) [netty-transport-4.1.11.Final.jar:4.1.11.Final] at io.netty.channel.nio.NioEventLoop.processSelectedKeysPlain(NioEventLoop.java:544) [netty-transport-4.1.11.Final.jar:4.1.11.Final] at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:498) [netty-transport-4.1.11.Final.jar:4.1.11.Final] at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:458) [netty-transport-4.1.11.Final.jar:4.1.11.Final] at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858) [netty-common-4.1.11.Final.jar:4.1.11.Final] at java.lang.Thread.run(Thread.java:745) [?:1.8.0_112]使用连接池后,避免了此类异常。