Vision Statement
Build 7.3.x elasticsearch embedded server into
How It Works
Elasticsearch embedded server starts a single Node
Elasticsearch client can be configured to access 9200
http://localhost:9200
Elasticsearch embedded server will create data and home temp folders that will be deleted
Code
Here is the main class that implements elasticsearch embedded server node
public class EmbeddedElasticSearchServer implements IOData { private static final Logger LOGGER = LoggerFactory.getLogger(EmbeddedElasticSearchServer.class); private Node instance; private int port; private final AnyConfig config; public EmbeddedElasticSearchServer(AnyConfig config) { this.config = config; } private static class PluginConfigurableNode extends Node { PluginConfigurableNode(Settings input, Map<String, String> properties, Path configPath, Supplier<String> defaultNodeName, Collection<Class<? extends Plugin>> classpathPlugins) { super(InternalSettingsPreparer.prepareEnvironment(input, properties, configPath, defaultNodeName), classpathPlugins, false); } } @Override public synchronized Completable start() { return new RunnableCommand<Void>("cmd-start") { @Override protected Void run() { Settings settings = getSettings(); instance = new PluginConfigurableNode(settings, ImmutableMap.of(), null, () -> config.getString(EsConfig.CLUSTER_NAME), singletonList(Netty4Plugin.class)); try { instance.start(); Runtime.getRuntime().addShutdownHook(new Thread(() -> { try { if (instance != null) { instance.close(); } } catch (IOException e) { LOGGER.error("Error closing ElasticSearch"); } })); LOGGER.info("ElasticSearch cluster {} started in local mode on port {}", instance.settings().get("cluster.name"), port); return null; } catch (NodeValidationException e) { throw new IZapException("Failed to start embedded elastic search server", e); } } }.toCompletable(); } @Override public synchronized Completable stop() { return new RunnableCommand<Void>("cmd-stop") { @Override protected Void run() { if (instance != null && !instance.isClosed()) { LOGGER.info("Stopping Elastic Search"); try { instance.close(); instance = null; LOGGER.info("Elastic Search on port {} stopped", port); } catch (IOException e) { throw new IZapException("Failed to close elastic search embedded server", e); } } return null; } }.toCompletable(); } private Settings getSettings() { String clusterName = config.getString(EsConfig.CLUSTER_NAME); String host = config.getString(EsConfig.HOST); port = config.getInt(EsConfig.PORT); try { File dataDir = Files.createTempDirectory(clusterName + "_" + System.currentTimeMillis() + "data").toFile(); FileUtils.forceDeleteOnExit(dataDir); cleanDataDir(dataDir.getAbsolutePath()); File homeDir = Files.createTempDirectory(clusterName + "_" + System.currentTimeMillis() + "-home").toFile(); cleanDataDir(homeDir.getAbsolutePath()); FileUtils.forceDeleteOnExit(homeDir); Settings.Builder settingsBuilder = Settings.builder() .put("cluster.name", clusterName) .put("http.host", host) .put("http.port", port) .put("transport.tcp.port", port + 100) .put(EsConfig.DATA_PATH.getName(), dataDir.getAbsolutePath()) .put(EsConfig.HOME_PATH.getName(), homeDir.getAbsolutePath()) .put("http.cors.enabled", true) .put("node.data", true) .put("http.type", "netty4") .put("transport.type", "netty4"); return settingsBuilder.build(); } catch (IOException e) { throw new IZapException("Failed to create temp data/home dir.", e); } } }
Elasticsearch embedded server implementation is using start
methods. Please note that RunnableCommand returns Completable and not Observable. stop
Observable<Void>
/start
methods have no return values. Here is the unit test for the stop
public class EmbeddedElasticSearchServerTest { private EmbeddedElasticSearchServer server; @Before public void setup() { AnyConfig config = ConfigBuilder.builder(ConfigType.TYPE_SAFE) .setFileName(this.getClass() .getResource("/es-config.properties").getFile()) .build(); server = new EmbeddedElasticSearchServer(config); } @Test public void startTest() { server.start().blockingGet(); server.stop().blockingGet(); } }
Elasticsearch dependencies
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.3.1</version> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>7.3.1</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>7.3.1</version> </dependency>
I simply had to thank you so much again. I’m not certain what I would have taken care of without the entire concepts contributed by you concerning that industry. This was a troublesome crisis in my circumstances, however , taking note of the expert fashion you solved it forced me to leap over fulfillment. Now i’m grateful for the guidance and in addition expect you recognize what a great job you are always carrying out educating people thru your website. I know that you haven’t met any of us.
I wish to express some thanks to you just for rescuing me from this condition. After browsing throughout the internet and seeing recommendations that were not pleasant, I figured my life was over. Being alive without the answers to the problems you’ve sorted out all through your entire post is a crucial case, as well as ones that might have badly damaged my entire career if I had not noticed your web page. Your good mastery and kindness in handling all the details was helpful. I’m not sure what I would’ve done if I hadn’t come across such a step like this. I’m able to at this moment look ahead to my future. Thanks so much for the skilled and effective guide. I won’t think twice to recommend your blog to any individual who needs and wants assistance on this area.