Hi all, recently I added an embedded ElasticSearch server for the upcoming release of AdroitLogic UltraESB. Embedded ES Server will be useful when a large ES cluster is not required and also for ES integration tests. Let’s have a look at the code.
import org.adroitlogic.logging.api.Logger;
import org.adroitlogic.logging.api.LoggerFactory;
import org.adroitlogic.ultraesb.api.monitoring.StatisticsServer;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.node.Node;
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
/**
* @author rajind
* Implementation of embedded elasticsearch server.
*
*/
public class ElasticSearchStatisticsServer implements StatisticsServer {
private Node node;
private static final Logger logger= LoggerFactory.getLogger(ElasticSearchStatisticsServer.class);
private static final String ES_PROPERTY_PATH_HOME = "path.home";
private static final String ES_PROPERTY_PATH_CONF = "path.conf";
/**
* Create an elasticsearch embedded node with provided default configuration.
*/
@SuppressWarnings("UnusedDeclaration")
public ElasticSearchStatisticsServer() {
}
@Override
public void init(String confDir, String homeDir) {
try {
logger.info("Embedded Elasticsearch server is initializing");
Settings elasticsearchSetting = Settings.settingsBuilder()
.put(ES_PROPERTY_PATH_HOME, homeDir)
.put(ES_PROPERTY_PATH_CONF, confDir)
.build();
node = nodeBuilder().settings(elasticsearchSetting).build().start();
} catch (Exception e) {
logger.warn("Could not init embedded elasticsearch node", e);
}
}
public Client getClient() {
if (node != null) {
return node.client();
} else {
return null;
}
}
@Override
public void destroy() {
try {
logger.info("Stopping embedded elasticsearch server");
node.close();
} catch(Exception e) {
logger.warn("Could not stop embedded elasticsearch node due to {}", e.getMessage());
}
}
}
It’s actually very simple. “path.conf” will contain the elasticsearch configuration yaml file “elasticsearch.yml“. Please remember that “path.home” will always have to be set as elasticsearch looks for that by default.
Cheers! 🙂
~Rajind Ruparathna