2017-02-16 731 views
1

我在HttpAsyncRequestExecutor上有問題。Elasticsearch Rest客戶端 - 調用performRequestAsync時ConnectionClosedException

我使用elasticsearch Java Rest Client,我總是得到一個ConnectionClosedException(見下文),當我打電話performRequestAsync

// variables (all with valid format): 
    // endpoint is just a List<String> with "14655/_search" 

    // params is just a Map<String, String> with 
    // "pretty", "true" 
    // "search_type", "query_then_fetch" 

    // entity is just a HttpEntity entity with the Json body request 

    final int numRequests = endpoints.size(); 
    final CountDownLatch latch = new CountDownLatch(numRequests); 
    try (Timer.Context ctx = this.requestTimer.time()) { 
     for (final String endpoint : endpoints) { 
      // ERROR hapens here: 
      restClient.performRequestAsync("GET", endpoint, params, entity, 
        new ResponseListener() { 

         @Override 
         public void onSuccess(final Response response) { 
          if (response != null) { 
           responses.add(response); 
           latch.countDown(); 
          } 
         } 

         @Override 
         public void onFailure(final Exception exception) { 
          latch.countDown(); 

          logger.error("could not get search results for....",exception); 
          exception.printStackTrace(); 
         } 
        }); 
     } 
    } 

例外這裏:

org.apache.http.ConnectionClosedException: Connection closed 
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.endOfInput(HttpAsyncRequestExecutor.java:341) 
at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:263) 
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:81) 
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:39) 
at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:116) 
at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:164) 
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:339) 
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:317) 
at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:278) 
at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:106) 
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:590) 
at java.lang.Thread.run(Thread.java:745) 

我不知道是什麼是連接關閉的真正原因。同樣的請求在kopf中運行良好,並返回有效的搜索結果。 另外,我不會撥打任何restClient.close()或任何類似的東西。

問題可能出在哪裏? 輸入的結尾是否導致關閉連接狀態(根據org.apache.http.nio.protocol.HttpAsyncRequestExecutor.endOfInput(conn))?如果是的話,那是什麼意思?

感謝

UPDATE:

我懷疑(即返回結果)的問題與Tomcat的HttpClient,因爲這段代碼工作正常在集成測試......但事實並非如此工作(獲得相同的ConnectionClosedException),當我通過一個tomcat部署的「接口」發出REST請求時

這個任何指示燈?

回答

2

問題是端口錯了。對於REST請求,端口應該是9200(而不是像它配置的那樣是9300)。 More info on elasticsearch ports.

我希望elasticsearch能製作一個更明確的錯誤日誌或者提示,比如「你在連接正確的端口嗎?」因爲當人們試圖用內置客戶端以外的任何東西訪問9300端口時。