package io.monokkel.actors; import akka.actor.Props; import akka.actor.UntypedActor; import io.monokkel.messages.ElasticNodeIsStarted; import io.monokkel.messages.StartElastic; import org.elasticsearch.node.Node; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import static org.elasticsearch.node.NodeBuilder.nodeBuilder; /** * Created by tarjei on 24/06/14. * * An actor that starts an embedded ElasticSearch node. The node reads configuration from * the classpath: elasticsearch.yml * */ public class ElasticNodeActor extends UntypedActor { private Node node; private Logger log = LoggerFactory.getLogger(ElasticNodeActor.class); public static Props props() { return Props.create(ElasticNodeActor.class, ElasticNodeActor::new); } @Override public void onReceive(Object message) throws Exception { if(message instanceof StartElastic){ startNode(); getSender().tell(new ElasticNodeIsStarted(),getSelf()); } else { unhandled(message); } } private void startNode() { node = nodeBuilder().node(); } @Override public void postRestart(Throwable reason) throws Exception { log.info("The ElasticNode actor is restarted. Trying to restart the elastic node"); startNode(); super.postRestart(reason); } @Override public void postStop() throws Exception { log.info("The ElasticNode is stopped by the actor system"); if(node != null){ // Since each node is initialized by the actor it is null checked node.close(); } super.postStop(); } }