/*
 * LICENCE : CloudUnit is available under the GNU Affero General Public License : https://gnu.org/licenses/agpl.html
 * but CloudUnit is licensed too under a standard commercial license.
 * Please contact our sales team if you would like to discuss the specifics of our Enterprise license.
 * If you are not sure whether the AGPL is right for you,
 * you can always test our software under the AGPL and inspect the source code before you contact us
 * about purchasing a commercial license.
 *
 * LEGAL TERMS : "CloudUnit" is a registered trademark of Treeptik and can't be used to endorse
 * or promote products derived from this project without prior written permission from Treeptik.
 * Products or services derived from this software may not be called "CloudUnit"
 * nor may "Treeptik" or similar confusing terms appear in their names without prior written permission.
 * For any questions, contact us : contact@treeptik.fr
 */

package fr.treeptik.cloudunit.monitor.tasks;

import fr.treeptik.cloudunit.monitor.Task;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Created by nicolas on 27/11/2015.
 */
@Component
public class ElasticsearchTask implements Task {

    private final static Logger logger = LoggerFactory.getLogger(ElasticsearchTask.class);

    private static final Integer DEFAULT_ES_PORT = 9310;

    @Value("${elasticsearch.ip}")
    private String ipES;

    @Override
    public void run() {
        TransportClient client = new TransportClient();
        try {
            client.addTransportAddress(new InetSocketTransportAddress(ipES, DEFAULT_ES_PORT));
            SearchRequestBuilder searchRequestBuilder = client.prepareSearch();
            searchRequestBuilder.setSize(1);
            SearchResponse response = searchRequestBuilder.execute().actionGet();
        } catch (Exception e) {
            StringBuilder msgError = new StringBuilder(1024);
            msgError.append("\n****************************************************************");
            msgError.append("\n****************************************************************");
            msgError.append("\n**                                                            **");
            msgError.append("\n   FATAL : ELASTICSEARCH NOT RUNNING : " + ipES + ":" + DEFAULT_ES_PORT);
            msgError.append("\n**                                                            **");
            msgError.append("\n****************************************************************");
            msgError.append("\n****************************************************************");
            logger.error(msgError.toString());
            System.exit(-1);
        } finally {
            if (client != null) {
                client.close();
            }
        }

    }


}

@@ -1379,16 +1379,76 @@
 .Value;%0A
+import org.springframework.scheduling.annotation.Scheduled;%0A
 import o
@@ -1488,16 +1488,16 @@
 ponent;%0A
-
 %0A/**%0A * 
@@ -1740,17 +1740,17 @@
 ORT = 93
-1
+0
 0;%0A%0A    
@@ -1839,24 +1839,723 @@
 oid run() %7B%0A
+        isRunning(true);%0A    %7D%0A%0A    @Scheduled(fixedDelay = 5000)%0A    private void monitor() %7B%0A        isRunning(false);%0A    %7D%0A%0A    /**%0A     * To evaluate if service is runnning or not%0A     * If needed, we stop the jvm or send an email%0A     *%0A     * @param exit%0A     */%0A    private void isRunning(boolean exit) %7B%0A        System.out.println(%22*********************************************%22);%0A        System.out.println(%22*********************************************%22);%0A        System.out.println(%22*********************************************%22);%0A        System.out.println(%22*********************************************%22);%0A        System.out.println(%22*********************************************%22);%0A%0A
         Tran
@@ -3342,16 +3342,8 @@
 %5Cn  
- FATAL :
  ELA
@@ -3402,16 +3402,306 @@
 _PORT);%0A
+            if (exit) %7B%0A                System.exit(-1);%0A                msgError.append(%22%5Cn**   FATAL ERROR : JVM IS KILLED                          **%22);%0A            %7D else %7B%0A                msgError.append(%22%5Cn**   WARNING ERROR : MANUAL ACTION REQUIRED               **%22);%0A            %7D%0A
         
@@ -3981,32 +3981,32 @@
 ************%22);%0A
+
             logg
@@ -4040,37 +4040,8 @@
 ));%0A
-            System.exit(-1);%0A
     

Add Test at boot for Elastichsearch