Advertisement
jillesvangurp

ESLauncher

Mar 5th, 2013
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. import static org.elasticsearch.common.settings.ImmutableSettings.Builder.EMPTY_SETTINGS;
  2.  
  3. import java.io.IOException;
  4. import java.util.concurrent.CountDownLatch;
  5.  
  6. import org.elasticsearch.Version;
  7. import org.elasticsearch.node.Node;
  8. import org.elasticsearch.node.NodeBuilder;
  9.  
  10. /**
  11. * Elastic search launcher for usage in tests. Also has a main method if you want to run ES from the IDE.
  12. */
  13. public class EsLauncher {
  14.  
  15. private Node node;
  16.  
  17. /**
  18. * Initialize and start the elastic search node. Set system properties to configure or call inMemorySettings().
  19. */
  20. public void start() {
  21. try {
  22. // This step actually interprets the system properties
  23. NodeBuilder nodeBuilder = NodeBuilder.nodeBuilder().settings(EMPTY_SETTINGS).loadConfigSettings(false);
  24. node = nodeBuilder.build();
  25. // register a shutdown hook
  26. Runtime.getRuntime().addShutdownHook(new Thread() {
  27. @Override
  28. public void run() {
  29. node.close();
  30. }
  31. });
  32. node.start();
  33. } catch (Exception e) {
  34. throw new IllegalStateException("elastic search failed to launch " + e.getMessage(), e);
  35. }
  36. }
  37.  
  38. /**
  39. * Sets a few system properties that Elastic Search uses to configure itself. You can override any property in the
  40. * yml file in a elastic search distribution by prepending es. and making it a system property
  41. */
  42. public void inMemorySettings() {
  43. System.setProperty("es.name", "devnode");
  44. System.setProperty("es.cluster.name", "localstream-dev");
  45. System.setProperty("es.index.store.type", "memory");
  46. System.setProperty("es.index.store.fs.memory.enabled", "memory");
  47. System.setProperty("es.index.gateway.type", "none");
  48. System.setProperty("es.gateway.type", "none");
  49. System.setProperty("es.discovery.zen.ping.multicast.enabled", "false");
  50. System.setProperty("es.path.data", "target/data");
  51. System.setProperty("es.path.logs", "target/logs");
  52. System.setProperty("es.foreground", "true");
  53. }
  54.  
  55. /**
  56. * Shut down the elastic search node.
  57. * @throws IOException
  58. */
  59. public void close() throws IOException {
  60. node.close();
  61. }
  62.  
  63. public static void main(String[] args) throws Exception {
  64. final CountDownLatch keepAliveLatch = new CountDownLatch(1);
  65. // keep this thread alive (non daemon thread) until we shutdown
  66. Runtime.getRuntime().addShutdownHook(new Thread() {
  67. @Override
  68. public void run() {
  69. keepAliveLatch.countDown();
  70. }
  71. });
  72.  
  73. Thread keepAliveThread = new Thread(new Runnable() {
  74. @Override
  75. public void run() {
  76. try {
  77. EsLauncher l = new EsLauncher();
  78. l.inMemorySettings();
  79. l.start();
  80. keepAliveLatch.await();
  81. l.close();
  82. } catch (InterruptedException e) {
  83. // bail out
  84. } catch (IOException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88. }, "elasticsearch[keepAlive/" + Version.CURRENT + "]");
  89. keepAliveThread.setDaemon(false);
  90. keepAliveThread.start();
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement