修復(fù)jest的Connection is still allocated異常

在springboot1.4.5版本下引用jest去訪問es,出現(xiàn)Connection is still allocated異常扑毡。

異常

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.java

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;
    }

修復(fù)

升級springboot到1.5.0.M1以上,后者自己修改auto config

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();
    }

設(shè)置multiThreaded(true)

doc

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末域携,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌策幼,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件奴紧,死亡現(xiàn)場離奇詭異特姐,居然都是意外死亡,警方通過查閱死者的電腦和手機黍氮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進店門唐含,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人沫浆,你說我怎么就攤上這事捷枯。” “怎么了专执?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵淮捆,是天一觀的道長。 經(jīng)常有香客問我本股,道長攀痊,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任拄显,我火速辦了婚禮苟径,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘躬审。我一直安慰自己棘街,他們只是感情好,可當我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布承边。 她就那樣靜靜地躺著遭殉,像睡著了一般。 火紅的嫁衣襯著肌膚如雪炒刁。 梳的紋絲不亂的頭發(fā)上恩沽,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天,我揣著相機與錄音翔始,去河邊找鬼罗心。 笑死,一個胖子當著我的面吹牛城瞎,可吹牛的內(nèi)容都是我干的渤闷。 我是一名探鬼主播,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼脖镀,長吁一口氣:“原來是場噩夢啊……” “哼飒箭!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤弦蹂,失蹤者是張志新(化名)和其女友劉穎肩碟,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體凸椿,經(jīng)...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡削祈,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了脑漫。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片髓抑。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖优幸,靈堂內(nèi)的尸體忽然破棺而出吨拍,到底是詐尸還是另有隱情,我是刑警寧澤网杆,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布羹饰,位于F島的核電站,受9級特大地震影響跛璧,放射性物質(zhì)發(fā)生泄漏严里。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一追城、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧燥撞,春花似錦座柱、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至冠胯,卻和暖如春火诸,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背荠察。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工置蜀, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人悉盆。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓盯荤,卻偏偏與公主長得像,于是被迫代替她去往敵國和親焕盟。 傳聞我的和親對象是個殘疾皇子秋秤,可洞房花燭夜當晚...
    茶點故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內(nèi)容