I have a singleton wrapper class abstracting elasticsearch APIs for my application.
public class ElasticSearchClient {
private static volatile ElasticSearchClient elasticSearchClientInstance;
private static final Object lock = new Object();
private static elasticConfig ;
/*
** Private constructor to make this class singleton
*/
private ElasticSearchClient() {
}
/*
** This method does a lazy initialization and returns the singleton instance of ElasticSearchClient
*/
public static ElasticSearchClient getInstance() {
ElasticSearchClient elasticSearchClientInstanceToReturn = elasticSearchClientInstance;
if (elasticSearchClientInstanceToReturn == null) {
synchronized(lock) {
elasticSearchClientInstanceToReturn = elasticSearchClientInstance;
if (elasticSearchClientInstanceToReturn == null) {
// While this thread was waiting for the lock, another thread may have instantiated the clinet.
elasticSearchClientInstanceToReturn = new ElasticSearchClient();
elasticSearchClientInstance = elasticSearchClientInstanceToReturn;
}
}
}
return elasticSearchClientInstanceToReturn;
}
/*
** This method creates a new elastic index with the name as the paramater, if if does not already exists.
* Returns true if the index creation is successful, false otherwise.
*/
public boolean createElasticIndex(String index) {
if (checkIfElasticSearchIndexExists(index)) {
LOG.error("Cannot recreate already existing index: " + index);
return false;
}
if (elasticConfig == null || elasticConfig.equals(BatchConstants.EMPTY_STRING)) {
loadElasticConfigFromFile(ELASTIC_CONFIG_FILE_NAME);
}
if (elasticConfig != null && !elasticConfig.equals("")) {
try {
HttpURLConnection elasticSearchHttpURLConnection = performHttpRequest(
ELASTIC_SEARCH_URL + "/" + index,
"PUT",
elasticConfig,
"Create index: " + index
);
return elasticSearchHttpURLConnection != null &&
elasticSearchHttpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK;
} catch (Exception e) {
LOG.error("Unable to access Elastic Search API. Following exception occurred:\n" + e.getMessage());
}
} else {
LOG.error("Found empty config file");
}
return false;
}
private void loadElasticConfigFromFile(String filename) {
try {
Object obj = jsonParser.parse(new FileReader(filename);
JSONObject jsonObject = (JSONObject) obj;
LOG.info("Successfully parsed elastic config file: "+ filename);
elasticConfig = jsonObject.toString();
return;
} catch (Exception e) {
LOG.error("Cannot read elastic config from " + filename + "\n" + e.getMessage());
elasticConfig = "";
}
}
}
I have multiple threads that use ElasticSearchClient as mentioned below
Thread1
ElasticSearchClient elasticSearchClient = ElasticSearchClient.getInstance()
elasticSearchClient.createElasticIndex("firstindex");
Thread2
ElasticSearchClient elasticSearchClient = ElasticSearchClient.getInstance()
elasticSearchClient.createElasticIndex("secondindex");
Thread3...
As per me the Singleton class is thread safe but I am not sure what will happen if more than one thread starts executing the same method of a singleton class. Does this have any side-effect?
Note: I am aware of that the singleton class above is not reflection and serialization safe.