Reputation: 29349
I am using ElasticSearch
Java client to query elastic search. I am initializing transport client every time I have to make a call. Is this the right way or should I initialize once during the start of the application and close it at shutdown.
Following is the code to initialize client
Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", Config.getEsClusterName()).put("client.transport.ignore_cluster_name", true).build();
Client esClient = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress(Config.getEsHost(), Config.getEsPort()));
Upvotes: 6
Views: 3615
Reputation: 1
TransportClient maintains a pool of connections inside, they'll be kept alive during the entire life cycle of the TransportClient object.
So you should use a static to store the TransportClient object, or implement a singleton class that handles the connection. The rest of your code will communicate with this class only.
Upvotes: 0
Reputation: 5737
I created a github repository for usage of java elasticsearch transport client[with singleton design pattern].. please ..make use of it.refer
Upvotes: 4
Reputation: 2762
The elasticsearch Java client is multithreaded and each new instance has a large overhead.
This should be instantiated once at the start of your program and shared across all callers.
Best Regards
Upvotes: 20