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. Function: HTTP connection pool */public class Httppoolmanager {public static Poolinghttpclientconnectionmanager Clientconnectionm
Anager = 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; * * Get instance 1/public synchronized static Httppoolmanager getinstance () {if (Poolmanager = = null) {Clientconnec TionmanaGER = new Poolinghttpclientconnectionmanager ();
Poolmanager = new Httppoolmanager ();
return poolmanager; /* * Get instance 1/public synchronized static Httppoolmanager getinstance (int maxtotal, int defaultmaxperroute) {if (Poolmanager = null)
{Poolmanager = new Httppoolmanager (maxtotal, Defaultmaxperroute);
return poolmanager; /* * Get closeablehttpclient/public static closeablehttpclient Gethttpclient () {if (Clientconnectionmanager =
= null) {Clientconnectionmanager = new Poolinghttpclientconnectionmanager ();
GetInstance ();
Return to Httpclients.custom (). Setconnectionmanager (Clientconnectionmanager). build (); }
}
Why use the HttpClient connection pool.
Recently in the use of ES for Full-text search, the use of the RESTful API operation, initially did not use the HttpClient connection pool, which causes each request to rebuild a httpclient instance, ES server reported the following exception:
[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: The remote host forced the shutdown of an existing connection.
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]
This class of exceptions is avoided after using connection pooling.