Hi,
Let's look at this class:
public class ESConnector { private Client client;
public Client getClient() { if(client == null) { client = createClient(); } return client; } protected Client createClient() { if(client == null) { try { Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "test").build(); TransportClient transportClient = new TransportClient(settings);
transportClient = transportClient.addTransportAddress(new InetSocketTransportAddress("localhost", 9300)); client = transportClient; } catch(Exception ex) { logger.error(ex, ex); } } return client;
}
This class is from some example. It is a singleton, so every time I want to search something in realtime I don't need to connecto to the Elasticsearch server. I wonder if it can be used in real production environment?
What if:
- There is restart of Elasticsearch server? Would this connection be able to search something after the restart?
- There are many threads using this connection - would they wait for each other and how to cope with this?
- There are couple of nodes in cluster
How good connector class would look like? Maybe you could share some example basing on your code that is proven on production?