Elasticsearch に接続するサーブレットを使用して検索 Web アプリケーションを構築しています。ElasticsearchのTransportモジュールについて質問があります。ServletContextListener を実装するクラスで TransportClient を使用して、Elasticsearch への接続を開いています。以下は、ElasticsearchServletContextListener クラスのコードです。
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にクエリを実行するために個別のクライアント接続が必要ですか? ご協力いただきありがとうございます。