在springboot1.4.5版本下引用jest去訪問es,出現Connection is still allocated異常。java
java.lang.IllegalStateException: Connection is still allocated at org.apache.http.util.Asserts.check(Asserts.java:34) at org.apache.http.impl.conn.BasicHttpClientConnectionManager.getConnection(BasicHttpClientConnectionManager.java:251) at org.apache.http.impl.conn.BasicHttpClientConnectionManager$1.get(BasicHttpClientConnectionManager.java:202) at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:191) at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185) at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89) at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111) at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108) at io.searchbox.client.http.JestHttpClient.executeRequest(JestHttpClient.java:118) at io.searchbox.client.http.JestHttpClient.execute(JestHttpClient.java:57)
spring-boot-autoconfigure-1.4.5.RELEASE-sources.jar!/org/springframework/boot/autoconfigure/elasticsearch/jest/JestAutoConfiguration.java
默認配置沒有啓用multiThread支持
jest-2.4.0-sources.jar!/io/searchbox/client/JestClientFactory.javagit
protected HttpClientConnectionManager getConnectionManager() { HttpClientConnectionManager retval; Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", httpClientConfig.getPlainSocketFactory()) .register("https", httpClientConfig.getSslSocketFactory()) .build(); if (httpClientConfig.isMultiThreaded()) { log.info("Using multi thread/connection supporting pooling connection manager"); final PoolingHttpClientConnectionManager poolingConnMgr = new PoolingHttpClientConnectionManager(registry); final Integer maxTotal = httpClientConfig.getMaxTotalConnection(); if (maxTotal != null) { poolingConnMgr.setMaxTotal(maxTotal); } final Integer defaultMaxPerRoute = httpClientConfig.getDefaultMaxTotalConnectionPerRoute(); if (defaultMaxPerRoute != null) { poolingConnMgr.setDefaultMaxPerRoute(defaultMaxPerRoute); } final Map<HttpRoute, Integer> maxPerRoute = httpClientConfig.getMaxTotalConnectionPerRoute(); for (Map.Entry<HttpRoute, Integer> entry : maxPerRoute.entrySet()) { poolingConnMgr.setMaxPerRoute(entry.getKey(), entry.getValue()); } retval = poolingConnMgr; } else { log.info("Using single thread/connection supporting basic connection manager"); retval = new BasicHttpClientConnectionManager(registry); } return retval; }
升級springboot到1.5.0.M1以上,後者本身修改auto configgithub
protected HttpClientConfig createHttpClientConfig() { HttpClientConfig.Builder builder = new HttpClientConfig.Builder( this.properties.getUris()); if (StringUtils.hasText(this.properties.getUsername())) { builder.defaultCredentials(this.properties.getUsername(), this.properties.getPassword()); } String proxyHost = this.properties.getProxy().getHost(); if (StringUtils.hasText(proxyHost)) { Integer proxyPort = this.properties.getProxy().getPort(); Assert.notNull(proxyPort, "Proxy port must not be null"); builder.proxy(new HttpHost(proxyHost, proxyPort)); } Gson gson = this.gsonProvider.getIfUnique(); if (gson != null) { builder.gson(gson); } return builder.connTimeout(this.properties.getConnectionTimeout()) .readTimeout(this.properties.getReadTimeout()) .multiThreaded(true) .build(); }
設置multiThreaded(true)spring