1. 程式人生 > >httpclient連線池在ES Restful API請求中的應用

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]
使用連線池後,避免了此類異常。