Search in sources :

Example 1 with Vertx

use of io.vertx.rxjava.core.Vertx in project georocket by georocket.

the class ElasticsearchRunner method runElasticsearch.

/**
 * Run Elasticsearch
 * @param host the host Elasticsearch should bind to
 * @param port the port Elasticsearch should listen on
 * @param elasticsearchInstallPath the path where Elasticsearch is installed
 * @return an observable that emits exactly one item when Elasticsearch has started
 */
public Observable<Void> runElasticsearch(String host, int port, String elasticsearchInstallPath) {
    JsonObject config = vertx.getOrCreateContext().config();
    String storage = config.getString(ConfigConstants.STORAGE_FILE_PATH);
    String root = storage + "/index";
    return vertx.<Void>rxExecuteBlocking(f -> {
        log.info("Starting Elasticsearch ...");
        // get Elasticsearch executable
        String executable = FilenameUtils.separatorsToSystem(elasticsearchInstallPath);
        executable = FilenameUtils.concat(executable, "bin");
        if (SystemUtils.IS_OS_WINDOWS) {
            executable = FilenameUtils.concat(executable, "elasticsearch.bat");
        } else {
            executable = FilenameUtils.concat(executable, "elasticsearch");
        }
        // start Elasticsearch
        CommandLine cmdl = new CommandLine(executable);
        cmdl.addArgument("-Ecluster.name=georocket-cluster");
        cmdl.addArgument("-Enode.name=georocket-node");
        cmdl.addArgument("-Enetwork.host=" + host);
        cmdl.addArgument("-Ehttp.port=" + port);
        cmdl.addArgument("-Epath.data=" + root + "/data");
        executor = new DefaultExecutor();
        executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
        executor.setWatchdog(new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT));
        // set ES_JAVA_OPTS environment variable if necessary
        Map<String, String> env = new HashMap<>(System.getenv());
        if (!env.containsKey("ES_JAVA_OPTS")) {
            String javaOpts = config.getString(ConfigConstants.INDEX_ELASTICSEARCH_JAVA_OPTS);
            if (javaOpts != null) {
                env.put("ES_JAVA_OPTS", javaOpts);
            }
        }
        try {
            executor.execute(cmdl, env, new DefaultExecuteResultHandler() {

                @Override
                public void onProcessComplete(final int exitValue) {
                    log.info("Elasticsearch quit with exit code: " + exitValue);
                }

                @Override
                public void onProcessFailed(final ExecuteException e) {
                    if (!stopped) {
                        log.error("Elasticsearch execution failed", e);
                    }
                }
            });
            f.complete();
        } catch (IOException e) {
            f.fail(e);
        }
    }).toObservable();
}
Also used : Executor(org.apache.commons.exec.Executor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) NoStackTraceThrowable(io.vertx.core.impl.NoStackTraceThrowable) IOException(java.io.IOException) HashMap(java.util.HashMap) CommandLine(org.apache.commons.exec.CommandLine) LoggerFactory(io.vertx.core.logging.LoggerFactory) Observable(rx.Observable) Map(java.util.Map) ShutdownHookProcessDestroyer(org.apache.commons.exec.ShutdownHookProcessDestroyer) JsonObject(io.vertx.core.json.JsonObject) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteException(org.apache.commons.exec.ExecuteException) RxUtils(io.georocket.util.RxUtils) Logger(io.vertx.core.logging.Logger) SystemUtils(org.apache.commons.lang.SystemUtils) FilenameUtils(org.apache.commons.io.FilenameUtils) DefaultExecuteResultHandler(org.apache.commons.exec.DefaultExecuteResultHandler) ConfigConstants(io.georocket.constants.ConfigConstants) Vertx(io.vertx.rxjava.core.Vertx) CommandLine(org.apache.commons.exec.CommandLine) DefaultExecuteResultHandler(org.apache.commons.exec.DefaultExecuteResultHandler) DefaultExecutor(org.apache.commons.exec.DefaultExecutor) ExecuteWatchdog(org.apache.commons.exec.ExecuteWatchdog) JsonObject(io.vertx.core.json.JsonObject) ExecuteException(org.apache.commons.exec.ExecuteException) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) ShutdownHookProcessDestroyer(org.apache.commons.exec.ShutdownHookProcessDestroyer)

Example 2 with Vertx

use of io.vertx.rxjava.core.Vertx in project georocket by georocket.

the class ServiceTest method broadcast.

/**
 * Test if a message can be sent to all consumers
 * @param context the test context
 */
@Test
public void broadcast(TestContext context) {
    Vertx vertx = new Vertx(rule.vertx());
    Async aC = context.async();
    Async bC = context.async();
    Async cC = context.async();
    Async dC = context.async();
    String data = "special data";
    vertx.eventBus().consumer("a", h -> {
        context.assertEquals(data, h.body());
        aC.complete();
    });
    vertx.eventBus().consumer("a", h -> {
        context.assertEquals(data, h.body());
        bC.complete();
    });
    vertx.eventBus().consumer("b", h -> {
        context.assertEquals(data, h.body());
        cC.complete();
    });
    vertx.eventBus().consumer("b", h -> {
        context.assertEquals(data, h.body());
        dC.complete();
    });
    ServiceDiscovery discovery = ServiceDiscovery.create(vertx);
    Service.publishOnce("A", "a", discovery, vertx).flatMap(v -> Service.publishOnce("A", "b", discovery, vertx)).flatMapObservable(v -> Service.discover("A", discovery, vertx)).doOnTerminate(discovery::close).subscribe(service -> {
        service.broadcast(data);
    }, context::fail);
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) List(java.util.List) Rule(org.junit.Rule) RunWith(org.junit.runner.RunWith) ServiceDiscovery(io.vertx.rxjava.servicediscovery.ServiceDiscovery) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Record(io.vertx.servicediscovery.Record) Collectors(java.util.stream.Collectors) RunTestOnContext(io.vertx.ext.unit.junit.RunTestOnContext) Vertx(io.vertx.rxjava.core.Vertx) Async(io.vertx.ext.unit.Async) Vertx(io.vertx.rxjava.core.Vertx) ServiceDiscovery(io.vertx.rxjava.servicediscovery.ServiceDiscovery) Test(org.junit.Test)

Example 3 with Vertx

use of io.vertx.rxjava.core.Vertx in project georocket by georocket.

the class ServiceTest method publishOnce.

/**
 * Test if a service is really published only once
 * @param context the test context
 */
@Test
public void publishOnce(TestContext context) {
    Vertx vertx = new Vertx(rule.vertx());
    Async async = context.async();
    ServiceDiscovery discovery = ServiceDiscovery.create(vertx);
    Service.publishOnce("A", "A", discovery, vertx).flatMap(v -> Service.publishOnce("A", "A", discovery, vertx)).flatMap(v -> Service.publishOnce("A", "B", discovery, vertx)).flatMap(v -> {
        return discovery.rxGetRecords(record -> true);
    }).doAfterTerminate(discovery::close).subscribe(recordList -> {
        List<Record> lA = recordList.stream().filter(r -> r.getName().equals("A")).collect(Collectors.toList());
        context.assertEquals(2, lA.size());
        async.complete();
    }, context::fail);
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) List(java.util.List) Rule(org.junit.Rule) RunWith(org.junit.runner.RunWith) ServiceDiscovery(io.vertx.rxjava.servicediscovery.ServiceDiscovery) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Record(io.vertx.servicediscovery.Record) Collectors(java.util.stream.Collectors) RunTestOnContext(io.vertx.ext.unit.junit.RunTestOnContext) Vertx(io.vertx.rxjava.core.Vertx) Async(io.vertx.ext.unit.Async) Record(io.vertx.servicediscovery.Record) Vertx(io.vertx.rxjava.core.Vertx) ServiceDiscovery(io.vertx.rxjava.servicediscovery.ServiceDiscovery) Test(org.junit.Test)

Example 4 with Vertx

use of io.vertx.rxjava.core.Vertx in project georocket by georocket.

the class ServiceTest method discover.

/**
 * Test if a service can be discovered
 * @param context the test context
 */
@Test
public void discover(TestContext context) {
    Vertx vertx = new Vertx(rule.vertx());
    Async async = context.async();
    ServiceDiscovery discovery = ServiceDiscovery.create(vertx);
    Service.publishOnce("A", "a", discovery, vertx).flatMap(v -> Service.publishOnce("A", "b", discovery, vertx)).flatMapObservable(v -> Service.discover("A", discovery, vertx)).count().doOnTerminate(discovery::close).subscribe(count -> {
        context.assertEquals(2, count);
        async.complete();
    }, context::fail);
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) List(java.util.List) Rule(org.junit.Rule) RunWith(org.junit.runner.RunWith) ServiceDiscovery(io.vertx.rxjava.servicediscovery.ServiceDiscovery) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Record(io.vertx.servicediscovery.Record) Collectors(java.util.stream.Collectors) RunTestOnContext(io.vertx.ext.unit.junit.RunTestOnContext) Vertx(io.vertx.rxjava.core.Vertx) Async(io.vertx.ext.unit.Async) Vertx(io.vertx.rxjava.core.Vertx) ServiceDiscovery(io.vertx.rxjava.servicediscovery.ServiceDiscovery) Test(org.junit.Test)

Example 5 with Vertx

use of io.vertx.rxjava.core.Vertx in project georocket by georocket.

the class ServiceTest method send.

/**
 * Test if a message can be sent and if a consumer receives the message.
 * This method does not test if the message can be sent to only one
 * consumer (instead of all, see {@link #broadcast(TestContext)}),
 * because it's not possible to check if a consumer will not receive a
 * message before the asynchronous test ends.
 * @param context the test context
 */
@Test
public void send(TestContext context) {
    Vertx vertx = new Vertx(rule.vertx());
    Async a = context.async();
    String data = "special data";
    vertx.eventBus().consumer("a", h -> {
        context.assertEquals(data, h.body());
        a.complete();
    });
    ServiceDiscovery discovery = ServiceDiscovery.create(vertx);
    Service.publishOnce("A", "a", discovery, vertx).flatMapObservable(v -> Service.discover("A", discovery, vertx)).doOnTerminate(discovery::close).subscribe(service -> {
        service.broadcast(data);
        service.send(data);
    }, context::fail);
}
Also used : Async(io.vertx.ext.unit.Async) Vertx(io.vertx.rxjava.core.Vertx) ServiceDiscovery(io.vertx.rxjava.servicediscovery.ServiceDiscovery) Test(org.junit.Test)

Aggregations

Vertx (io.vertx.rxjava.core.Vertx)11 Async (io.vertx.ext.unit.Async)6 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 ServiceDiscovery (io.vertx.rxjava.servicediscovery.ServiceDiscovery)5 Test (org.junit.Test)5 TestContext (io.vertx.ext.unit.TestContext)4 RunTestOnContext (io.vertx.ext.unit.junit.RunTestOnContext)4 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)4 Record (io.vertx.servicediscovery.Record)4 IOException (java.io.IOException)4 Observable (rx.Observable)4 JsonObject (io.vertx.core.json.JsonObject)3 Splitter (com.google.common.base.Splitter)2 InputReader (de.undercouch.underline.InputReader)2 ArgumentType (de.undercouch.underline.Option.ArgumentType)2 OptionDesc (de.undercouch.underline.OptionDesc)2 OptionParserException (de.undercouch.underline.OptionParserException)2 UnknownAttributes (de.undercouch.underline.UnknownAttributes)2 GeoRocketClient (io.georocket.client.GeoRocketClient)2