1

我正在使用連接到Elasticsearch的Servlet構建搜索Web應用程序。我有一個關於Elasticsearch的Transport模塊的問題。我正在使用實現ServletContextListener的類中的TransportClient打開與Elasticsearch的連接。以下是ElasticsearchServletContextListener類的代碼。Elasticsearch傳輸客戶端連接

public class ElasticsearchContextListener implements ServletContextListener { 

    @Override 
    public void contextInitialized(ServletContextEvent servletContextEvent) { 
    System.out.println("Starting up!"); 

    try { 
     Client client = TransportClient.builder().build().addTransportAddress(
      new InetSocketTransportAddress(InetAddress.getByName("IP-address"),9300)); 
    //Storing the client connection in a static variable 
     Parameters.setESclient(client); 

    } catch (UnknownHostException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    } 

    @Override 
    public void contextDestroyed(ServletContextEvent servletContextEvent) { 
    Parameters.getESclient().close(); 
    System.out.println("Shutting down!"); 
    } 
} 

現在每當一個查詢它使用在了ServletContextListener類初始化相同的「客戶」連接的用戶搜索。客戶端連接可以同時處理多個請求嗎?或者每個用戶都需要單獨的客戶端連接來查詢elasticsearch?謝謝您的幫助。

回答

0

Client實例能夠處理多個調用並處理多個線程。而且您應該只有一個客戶端實例,因爲創建一個客戶端實例非常昂貴。

+0

謝謝Andrei的回覆。它可以同時處理的連接數有限制嗎? –

+1

可能是操作系統級別的限制。例如 - https://discuss.elastic.co/t/elasticsearch-throwing-outofmemoryerror-unable-to-create-new-native-thread-error/31686/7。此外,ES還有一些線程池和一些等待隊列用於各種操作(搜索,索引,批量索引等),但這是在服務器級別。所以,就你的情況而言,我認爲只有操作系統級限制是技術理論限制。 –

+0

謝謝Andrei的解釋。 –