package org.apache.mesos.elasticsearch.systemtest;

import org.apache.log4j.Logger;
import org.apache.mesos.mini.MesosCluster;
import org.apache.mesos.mini.mesos.MesosClusterConfig;

/**
 * Main app to run Mesos Elasticsearch with Mini Mesos.
 */
@SuppressWarnings({"PMD.AvoidUsingHardCodedIP"})
public class Main {

    public static final Logger LOGGER = Logger.getLogger(Main.class);

    public static void main(String[] args) throws InterruptedException {
        MesosClusterConfig config = MesosClusterConfig.builder()
                .numberOfSlaves(3)
                .privateRegistryPort(15000) // Currently you have to choose an available port by yourself
                .slaveResources(new String[]{"ports(*):[9200-9200,9300-9300]", "ports(*):[9201-9201,9301-9301]", "ports(*):[9202-9202,9302-9302]"})
                .build();

        MesosCluster cluster = new MesosCluster(config);
        cluster.start();
        cluster.injectImage("mesos/elasticsearch-executor");

        LOGGER.info("Starting scheduler");

        ElasticsearchSchedulerContainer scheduler = new ElasticsearchSchedulerContainer(config.dockerClient, cluster.getMesosContainer().getIpAddress());

        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                scheduler.remove();
                cluster.stop();
            }
        });

        scheduler.start();

        LOGGER.info("Scheduler started at http://" + scheduler.getIpAddress() + ":8080");

        LOGGER.info("Type CTRL-C to quit");
        while (true) {
            Thread.sleep(1000);
        }
    }

}

@@ -176,16 +176,69 @@
 onfig;%0A%0A
+import java.util.concurrent.atomic.AtomicReference;%0A%0A
 /**%0A * M
@@ -945,16 +945,431 @@
 onfig);%0A
+        final AtomicReference%3CElasticsearchSchedulerContainer%3E schedulerReference = new AtomicReference%3C%3E(null);%0A%0A        Runtime.getRuntime().addShutdownHook(new Thread() %7B%0A            @Override%0A            public void run() %7B%0A                if (schedulerReference.get() != null) %7B%0A                    schedulerReference.get().remove();%0A                %7D%0A                cluster.stop();%0A            %7D%0A        %7D);%0A
         
@@ -1645,25 +1645,24 @@
 ess());%0A
-%0A
         
 Runtime.
@@ -1657,205 +1657,40 @@
     
-Runtime.getRuntime().addShutdownHook(new Thread() %7B%0A            @Override%0A            public void run() %7B%0A                scheduler.remove();%0A                cluster.stop();%0A            %7D%0A        %7D
+schedulerReference.set(scheduler
 );%0A%0A

Make sure to properly shutdown cluster and collect logs even if scheduler fails