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();
}
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);
}
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);
}
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);
}
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);
}
Aggregations