Search in sources :

Example 6 with Vertx

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

the class ServiceTest method unpublish.

/**
 * Test if a service can be published an unpublished again
 * @param context the test context
 */
@Test
public void unpublish(TestContext context) {
    Vertx vertx = new Vertx(rule.vertx());
    Async async = context.async();
    ServiceDiscovery discovery = ServiceDiscovery.create(vertx);
    Service.publishOnce("A", "a", discovery, vertx).flatMapObservable(v -> Service.discover("A", discovery, vertx)).count().doOnNext(count -> {
        context.assertEquals(1, count);
    }).flatMap(v -> Service.discover("A", discovery, vertx)).flatMapSingle(service -> service.unpublish(discovery)).flatMap(v -> Service.discover("A", discovery, vertx)).count().doOnTerminate(discovery::close).subscribe(count -> {
        context.assertEquals(0, 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 7 with Vertx

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

the class ElasticsearchClientFactory method createElasticsearchClient.

/**
 * Create an Elasticsearch client. Either start an Elasticsearch instance or
 * connect to an external one - depending on the configuration.
 * @param indexName the name of the index the Elasticsearch client will
 * operate on
 * @return an observable emitting an Elasticsearch client and runner
 */
public Observable<ElasticsearchClient> createElasticsearchClient(String indexName) {
    JsonObject config = vertx.getOrCreateContext().config();
    boolean embedded = config.getBoolean(ConfigConstants.INDEX_ELASTICSEARCH_EMBEDDED, true);
    String host = config.getString(ConfigConstants.INDEX_ELASTICSEARCH_HOST, "localhost");
    int port = config.getInteger(ConfigConstants.INDEX_ELASTICSEARCH_PORT, 9200);
    ElasticsearchClient client = new RemoteElasticsearchClient(host, port, indexName, vertx);
    if (!embedded) {
        // just return the client
        return Observable.just(client);
    }
    return client.isRunning().flatMap(running -> {
        if (running) {
            // we don't have to start Elasticsearch again
            return Observable.just(client);
        }
        String home = config.getString(ConfigConstants.HOME);
        String defaultElasticsearchDownloadUrl;
        try {
            defaultElasticsearchDownloadUrl = IOUtils.toString(getClass().getResource("/elasticsearch_download_url.txt"), StandardCharsets.UTF_8);
        } catch (IOException e) {
            return Observable.error(e);
        }
        String elasticsearchDownloadUrl = config.getString(ConfigConstants.INDEX_ELASTICSEARCH_DOWNLOAD_URL, defaultElasticsearchDownloadUrl);
        Pattern pattern = Pattern.compile("-([0-9]\\.[0-9]\\.[0-9])\\.zip$");
        Matcher matcher = pattern.matcher(elasticsearchDownloadUrl);
        if (!matcher.find()) {
            return Observable.error(new NoStackTraceThrowable("Could not extract " + "version number from Elasticsearch download URL: " + elasticsearchDownloadUrl));
        }
        String elasticsearchVersion = matcher.group(1);
        String elasticsearchInstallPath = config.getString(ConfigConstants.INDEX_ELASTICSEARCH_INSTALL_PATH, home + "/elasticsearch/" + elasticsearchVersion);
        // install Elasticsearch, start it and then create the client
        ElasticsearchInstaller installer = new ElasticsearchInstaller(vertx);
        ElasticsearchRunner runner = new ElasticsearchRunner(vertx);
        return installer.download(elasticsearchDownloadUrl, elasticsearchInstallPath).flatMap(path -> runner.runElasticsearch(host, port, path)).flatMap(v -> runner.waitUntilElasticsearchRunning(client)).map(v -> new EmbeddedElasticsearchClient(client, runner));
    });
}
Also used : IOUtils(org.apache.commons.io.IOUtils) Matcher(java.util.regex.Matcher) NoStackTraceThrowable(io.vertx.core.impl.NoStackTraceThrowable) IOException(java.io.IOException) JsonObject(io.vertx.core.json.JsonObject) Pattern(java.util.regex.Pattern) StandardCharsets(java.nio.charset.StandardCharsets) ConfigConstants(io.georocket.constants.ConfigConstants) Vertx(io.vertx.rxjava.core.Vertx) Observable(rx.Observable) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) JsonObject(io.vertx.core.json.JsonObject) IOException(java.io.IOException) NoStackTraceThrowable(io.vertx.core.impl.NoStackTraceThrowable)

Example 8 with Vertx

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

the class StoreEndpointTest method setupServer.

/**
 * Starts a MockServer verticle with a StoreEndpoint to test against
 * @param context the test context
 */
@BeforeClass
public static void setupServer(TestContext context) {
    Async async = context.async();
    vertx = new Vertx(rule.vertx());
    vertxCore = vertx.getDelegate();
    setConfig(vertx.getOrCreateContext().config());
    setupMockEndpoint().subscribe(x -> async.complete());
}
Also used : Async(io.vertx.ext.unit.Async) Vertx(io.vertx.rxjava.core.Vertx) BeforeClass(org.junit.BeforeClass)

Example 9 with Vertx

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

the class MockIndexer method mockIndexerQuery.

/**
 * Start consuming {@link AddressConstants#INDEXER_QUERY} messages.
 * See the class comments to see the logic of the replied items.
 *
 * Returns "valid" hits that correspond to the items that are returned from the {@link MockStore}.
 *
 * @param vertx vertx instance
 */
public static void mockIndexerQuery(Vertx vertx) {
    indexerQuerySubscription = vertx.eventBus().<JsonObject>consumer(AddressConstants.INDEXER_QUERY).toObservable().subscribe(msg -> {
        JsonArray hits = new JsonArray();
        String givenScrollId = msg.body().getString("scrollId");
        Long numberReturnHits;
        String returnScrollId;
        if (givenScrollId == null) {
            numberReturnHits = HITS_PER_PAGE;
            returnScrollId = FIRST_RETURNED_SCROLL_ID;
        } else if (givenScrollId.equals(FIRST_RETURNED_SCROLL_ID)) {
            numberReturnHits = TOTAL_HITS - HITS_PER_PAGE;
            returnScrollId = INVALID_SCROLLID;
        } else {
            numberReturnHits = 0L;
            returnScrollId = INVALID_SCROLLID;
        }
        for (int i = 0; i < numberReturnHits; i++) {
            hits.add(new JsonObject().put("mimeType", "application/geo+json").put("id", "some_id").put("start", 0).put("end", MockStore.RETURNED_CHUNK.length()).put("parents", new JsonArray()));
        }
        if (INVALID_SCROLLID.equals(givenScrollId)) {
            msg.fail(404, "invalid scroll id");
        } else {
            msg.reply(new JsonObject().put("totalHits", TOTAL_HITS).put("scrollId", returnScrollId).put("hits", hits));
        }
    });
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) AddressConstants(io.georocket.constants.AddressConstants) Subscription(rx.Subscription) Vertx(io.vertx.rxjava.core.Vertx) JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject)

Example 10 with Vertx

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

the class ImportCommand method doRun.

@Override
public void doRun(String[] remainingArgs, InputReader in, PrintWriter out, Handler<Integer> handler) throws OptionParserException, IOException {
    long start = System.currentTimeMillis();
    // resolve file patterns
    Queue<String> queue = new ArrayDeque<>();
    for (String p : patterns) {
        // convert Windows backslashes to slashes (necessary for Files.newDirectoryStream())
        if (SystemUtils.IS_OS_WINDOWS) {
            p = FilenameUtils.separatorsToUnix(p);
        }
        // collect paths and glob patterns
        List<String> roots = new ArrayList<>();
        List<String> globs = new ArrayList<>();
        String[] parts = p.split("/");
        boolean rootParsed = false;
        for (String part : parts) {
            if (!rootParsed) {
                if (hasGlobCharacter(part)) {
                    globs.add(part);
                    rootParsed = true;
                } else {
                    roots.add(part);
                }
            } else {
                globs.add(part);
            }
        }
        if (globs.isEmpty()) {
            // string does not contain a glob pattern at all
            queue.add(p);
        } else {
            // string contains a glob pattern
            if (roots.isEmpty()) {
                // there are not paths in the string. start from the current
                // working directory
                roots.add(".");
            }
            // add all files matching the pattern
            String root = String.join("/", roots);
            String glob = String.join("/", globs);
            Project project = new Project();
            FileSet fs = new FileSet();
            fs.setDir(new File(root));
            fs.setIncludes(glob);
            DirectoryScanner ds = fs.getDirectoryScanner(project);
            Arrays.stream(ds.getIncludedFiles()).map(path -> Paths.get(root, path).toString()).forEach(queue::add);
        }
    }
    if (queue.isEmpty()) {
        error("given pattern didn't match any files");
        return;
    }
    Vertx vertx = new Vertx(this.vertx);
    GeoRocketClient client = createClient();
    int queueSize = queue.size();
    doImport(queue, client, vertx, exitCode -> {
        client.close();
        if (exitCode == 0) {
            String m = "file";
            if (queueSize > 1) {
                m += "s";
            }
            System.out.println("Successfully imported " + queueSize + " " + m + " in " + DurationFormat.formatUntilNow(start));
        }
        handler.handle(exitCode);
    });
}
Also used : AsyncFile(io.vertx.core.file.AsyncFile) Arrays(java.util.Arrays) OptionParserException(de.undercouch.underline.OptionParserException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayList(java.util.ArrayList) Observable(rx.Observable) Pair(org.apache.commons.lang3.tuple.Pair) FileSet(org.apache.tools.ant.types.FileSet) WriteStream(io.vertx.core.streams.WriteStream) FileSystem(io.vertx.rxjava.core.file.FileSystem) Project(org.apache.tools.ant.Project) UnknownAttributes(de.undercouch.underline.UnknownAttributes) Pump(io.vertx.core.streams.Pump) AsyncResult(io.vertx.core.AsyncResult) Splitter(com.google.common.base.Splitter) OptionDesc(de.undercouch.underline.OptionDesc) PrintWriter(java.io.PrintWriter) OpenOptions(io.vertx.core.file.OpenOptions) ObservableFuture(io.vertx.rx.java.ObservableFuture) SystemUtils(org.apache.commons.lang3.SystemUtils) InputReader(de.undercouch.underline.InputReader) DurationFormat(io.georocket.util.DurationFormat) IOException(java.io.IOException) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) GeoRocketClient(io.georocket.client.GeoRocketClient) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) File(java.io.File) List(java.util.List) Stream(java.util.stream.Stream) Buffer(io.vertx.core.buffer.Buffer) Paths(java.nio.file.Paths) RxHelper(io.vertx.rx.java.RxHelper) Optional(java.util.Optional) Queue(java.util.Queue) ArrayDeque(java.util.ArrayDeque) ArgumentType(de.undercouch.underline.Option.ArgumentType) Handler(io.vertx.core.Handler) FilenameUtils(org.apache.commons.io.FilenameUtils) Vertx(io.vertx.rxjava.core.Vertx) FileSet(org.apache.tools.ant.types.FileSet) ArrayList(java.util.ArrayList) Vertx(io.vertx.rxjava.core.Vertx) GeoRocketClient(io.georocket.client.GeoRocketClient) ArrayDeque(java.util.ArrayDeque) Project(org.apache.tools.ant.Project) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) AsyncFile(io.vertx.core.file.AsyncFile) File(java.io.File)

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