Search in sources :

Example 1 with Message

use of io.vertx.rxjava.core.eventbus.Message in project vertx-examples by vert-x3.

the class Sender method start.

@Override
public void start() throws Exception {
    EventBus eb = vertx.eventBus();
    vertx.setPeriodic(1000, v -> {
        // Send two messages expecting replies
        Single<Message<Integer>> reply1 = eb.<Integer>rxSend("heatsensor1", "ping");
        Single<Message<Integer>> reply2 = eb.<Integer>rxSend("heatsensor2", "ping");
        // Zip responses to receive both at the same time
        Single<int[]> reply = reply1.zipWith(reply2, (msg1, msg2) -> new int[] { msg1.body(), msg2.body() });
        reply.subscribe(heats -> {
            // Print highest temp
            if (heats[0] > heats[1]) {
                System.out.println("heat sensor 1 is highest " + heats[0]);
            } else {
                System.out.println("heat sensor 2 is highest " + heats[1]);
            }
        }, err -> {
            System.out.println("Reply error:");
            err.printStackTrace();
        });
    });
}
Also used : Message(io.vertx.rxjava.core.eventbus.Message) EventBus(io.vertx.rxjava.core.eventbus.EventBus)

Example 2 with Message

use of io.vertx.rxjava.core.eventbus.Message in project georocket by georocket.

the class IndexerVerticle method onAdd.

/**
 * Will be called when chunks should be added to the index
 * @param messages the list of add messages that contain the paths to
 * the chunks to be indexed
 * @return an observable that completes when the operation has finished
 */
private Observable<Void> onAdd(List<Message<JsonObject>> messages) {
    return Observable.from(messages).flatMap(msg -> {
        // get path to chunk from message
        JsonObject body = msg.body();
        String path = body.getString("path");
        if (path == null) {
            msg.fail(400, "Missing path to the chunk to index");
            return Observable.empty();
        }
        // get chunk metadata
        JsonObject meta = body.getJsonObject("meta");
        if (meta == null) {
            msg.fail(400, "Missing metadata for chunk " + path);
            return Observable.empty();
        }
        // get tags
        JsonArray tagsArr = body.getJsonArray("tags");
        List<String> tags = tagsArr != null ? tagsArr.stream().flatMap(o -> o != null ? Stream.of(o.toString()) : Stream.of()).collect(Collectors.toList()) : null;
        // get properties
        JsonObject propertiesObj = body.getJsonObject("properties");
        Map<String, Object> properties = propertiesObj != null ? propertiesObj.getMap() : null;
        // get fallback CRS
        String fallbackCRSString = body.getString("fallbackCRSString");
        log.trace("Indexing " + path);
        String correlationId = body.getString("correlationId");
        String filename = body.getString("filename");
        long timestamp = body.getLong("timestamp", System.currentTimeMillis());
        ChunkMeta chunkMeta = getMeta(meta);
        IndexMeta indexMeta = new IndexMeta(correlationId, filename, timestamp, tags, properties, fallbackCRSString);
        // open chunk and create IndexRequest
        return openChunkToDocument(path, chunkMeta, indexMeta).map(doc -> Tuple.tuple(path, new JsonObject(doc), msg)).onErrorResumeNext(err -> {
            msg.fail(throwableToCode(err), throwableToMessage(err, ""));
            return Observable.empty();
        });
    }).toList().flatMap(l -> {
        if (!l.isEmpty()) {
            return insertDocuments(TYPE_NAME, l);
        }
        return Observable.empty();
    });
}
Also used : JsonArray(io.vertx.core.json.JsonArray) MetaIndexer(io.georocket.index.xml.MetaIndexer) GeoJsonChunkMeta(io.georocket.storage.GeoJsonChunkMeta) IndexMeta(io.georocket.storage.IndexMeta) StreamEvent(io.georocket.util.StreamEvent) XMLChunkMeta(io.georocket.storage.XMLChunkMeta) XMLParserOperator(io.georocket.util.XMLParserOperator) ChunkMeta(io.georocket.storage.ChunkMeta) RxStore(io.georocket.storage.RxStore) Tuple2(org.jooq.lambda.tuple.Tuple2) Tuple3(org.jooq.lambda.tuple.Tuple3) JsonParserOperator(io.georocket.util.JsonParserOperator) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) Logger(io.vertx.core.logging.Logger) MetaIndexerFactory(io.georocket.index.xml.MetaIndexerFactory) Message(io.vertx.rxjava.core.eventbus.Message) ServiceLoader(java.util.ServiceLoader) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) List(java.util.List) ElasticsearchClientFactory(io.georocket.index.elasticsearch.ElasticsearchClientFactory) Stream(java.util.stream.Stream) Tuple(org.jooq.lambda.tuple.Tuple) MapUtils(io.georocket.util.MapUtils) Buffer(io.vertx.core.buffer.Buffer) MimeTypeUtils.belongsTo(io.georocket.util.MimeTypeUtils.belongsTo) RxHelper(io.vertx.rx.java.RxHelper) AddressConstants(io.georocket.constants.AddressConstants) ChunkReadStream(io.georocket.storage.ChunkReadStream) Operator(rx.Observable.Operator) HashMap(java.util.HashMap) Seq(org.jooq.lambda.Seq) DefaultQueryCompiler(io.georocket.query.DefaultQueryCompiler) LoggerFactory(io.vertx.core.logging.LoggerFactory) ArrayList(java.util.ArrayList) AbstractVerticle(io.vertx.rxjava.core.AbstractVerticle) Observable(rx.Observable) Func1(rx.functions.Func1) ImmutableList(com.google.common.collect.ImmutableList) XMLIndexerFactory(io.georocket.index.xml.XMLIndexerFactory) StoreFactory(io.georocket.storage.StoreFactory) JsonIndexerFactory(io.georocket.index.xml.JsonIndexerFactory) NoStackTraceThrowable(io.vertx.core.impl.NoStackTraceThrowable) ThrowableHelper.throwableToMessage(io.georocket.util.ThrowableHelper.throwableToMessage) StreamIndexer(io.georocket.index.xml.StreamIndexer) TimeUnit(java.util.concurrent.TimeUnit) JsonArray(io.vertx.core.json.JsonArray) ThrowableHelper.throwableToCode(io.georocket.util.ThrowableHelper.throwableToCode) ElasticsearchClient(io.georocket.index.elasticsearch.ElasticsearchClient) RxUtils(io.georocket.util.RxUtils) DefaultMetaIndexerFactory(io.georocket.index.generic.DefaultMetaIndexerFactory) ConfigConstants(io.georocket.constants.ConfigConstants) JsonChunkMeta(io.georocket.storage.JsonChunkMeta) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) IndexMeta(io.georocket.storage.IndexMeta) GeoJsonChunkMeta(io.georocket.storage.GeoJsonChunkMeta) XMLChunkMeta(io.georocket.storage.XMLChunkMeta) ChunkMeta(io.georocket.storage.ChunkMeta) JsonChunkMeta(io.georocket.storage.JsonChunkMeta)

Example 3 with Message

use of io.vertx.rxjava.core.eventbus.Message in project georocket by georocket.

the class IndexerVerticle method insertDocuments.

/**
 * Insert multiple Elasticsearch documents into the index. Perform a
 * bulk request. This method replies to all messages if the bulk request
 * was successful.
 * @param type Elasticsearch type for documents
 * @param documents a list of tuples containing document IDs, documents to
 * index, and the respective messages from which the documents were created
 * @return an observable that completes when the operation has finished
 */
private Observable<Void> insertDocuments(String type, List<Tuple3<String, JsonObject, Message<JsonObject>>> documents) {
    long startTimeStamp = System.currentTimeMillis();
    List<String> chunkPaths = Seq.seq(documents).map(Tuple3::v1).toList();
    onIndexingStarted(startTimeStamp, chunkPaths);
    List<Tuple2<String, JsonObject>> docsToInsert = Seq.seq(documents).map(Tuple3::limit2).toList();
    List<Message<JsonObject>> messages = Seq.seq(documents).map(Tuple3::v3).toList();
    return client.bulkInsert(type, docsToInsert).flatMap(bres -> {
        JsonArray items = bres.getJsonArray("items");
        for (int i = 0; i < items.size(); ++i) {
            JsonObject jo = items.getJsonObject(i);
            JsonObject item = jo.getJsonObject("index");
            Message<JsonObject> msg = messages.get(i);
            if (client.bulkResponseItemHasErrors(item)) {
                msg.fail(500, client.bulkResponseItemGetErrorMessage(item));
            } else {
                msg.reply(null);
            }
        }
        long stopTimeStamp = System.currentTimeMillis();
        List<String> correlationIds = Seq.seq(messages).map(Message::body).map(d -> d.getString("correlationId")).toList();
        onIndexingFinished(stopTimeStamp - startTimeStamp, correlationIds, chunkPaths, client.bulkResponseGetErrorMessage(bres));
        return Observable.empty();
    });
}
Also used : JsonArray(io.vertx.core.json.JsonArray) MetaIndexer(io.georocket.index.xml.MetaIndexer) GeoJsonChunkMeta(io.georocket.storage.GeoJsonChunkMeta) IndexMeta(io.georocket.storage.IndexMeta) StreamEvent(io.georocket.util.StreamEvent) XMLChunkMeta(io.georocket.storage.XMLChunkMeta) XMLParserOperator(io.georocket.util.XMLParserOperator) ChunkMeta(io.georocket.storage.ChunkMeta) RxStore(io.georocket.storage.RxStore) Tuple2(org.jooq.lambda.tuple.Tuple2) Tuple3(org.jooq.lambda.tuple.Tuple3) JsonParserOperator(io.georocket.util.JsonParserOperator) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) Logger(io.vertx.core.logging.Logger) MetaIndexerFactory(io.georocket.index.xml.MetaIndexerFactory) Message(io.vertx.rxjava.core.eventbus.Message) ServiceLoader(java.util.ServiceLoader) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) List(java.util.List) ElasticsearchClientFactory(io.georocket.index.elasticsearch.ElasticsearchClientFactory) Stream(java.util.stream.Stream) Tuple(org.jooq.lambda.tuple.Tuple) MapUtils(io.georocket.util.MapUtils) Buffer(io.vertx.core.buffer.Buffer) MimeTypeUtils.belongsTo(io.georocket.util.MimeTypeUtils.belongsTo) RxHelper(io.vertx.rx.java.RxHelper) AddressConstants(io.georocket.constants.AddressConstants) ChunkReadStream(io.georocket.storage.ChunkReadStream) Operator(rx.Observable.Operator) HashMap(java.util.HashMap) Seq(org.jooq.lambda.Seq) DefaultQueryCompiler(io.georocket.query.DefaultQueryCompiler) LoggerFactory(io.vertx.core.logging.LoggerFactory) ArrayList(java.util.ArrayList) AbstractVerticle(io.vertx.rxjava.core.AbstractVerticle) Observable(rx.Observable) Func1(rx.functions.Func1) ImmutableList(com.google.common.collect.ImmutableList) XMLIndexerFactory(io.georocket.index.xml.XMLIndexerFactory) StoreFactory(io.georocket.storage.StoreFactory) JsonIndexerFactory(io.georocket.index.xml.JsonIndexerFactory) NoStackTraceThrowable(io.vertx.core.impl.NoStackTraceThrowable) ThrowableHelper.throwableToMessage(io.georocket.util.ThrowableHelper.throwableToMessage) StreamIndexer(io.georocket.index.xml.StreamIndexer) TimeUnit(java.util.concurrent.TimeUnit) JsonArray(io.vertx.core.json.JsonArray) ThrowableHelper.throwableToCode(io.georocket.util.ThrowableHelper.throwableToCode) ElasticsearchClient(io.georocket.index.elasticsearch.ElasticsearchClient) RxUtils(io.georocket.util.RxUtils) DefaultMetaIndexerFactory(io.georocket.index.generic.DefaultMetaIndexerFactory) ConfigConstants(io.georocket.constants.ConfigConstants) JsonChunkMeta(io.georocket.storage.JsonChunkMeta) Message(io.vertx.rxjava.core.eventbus.Message) ThrowableHelper.throwableToMessage(io.georocket.util.ThrowableHelper.throwableToMessage) Tuple2(org.jooq.lambda.tuple.Tuple2) JsonObject(io.vertx.core.json.JsonObject)

Example 4 with Message

use of io.vertx.rxjava.core.eventbus.Message in project georocket by georocket.

the class ImporterVerticle method onImport.

/**
 * Receives a name of a file to import
 * @param msg the event bus message containing the filename
 * @return a single that will emit an item when the file has
 * been imported
 */
protected Single<Void> onImport(Message<JsonObject> msg) {
    JsonObject body = msg.body();
    String filename = body.getString("filename");
    String filepath = incoming + "/" + filename;
    String layer = body.getString("layer", "/");
    String contentType = body.getString("contentType");
    String correlationId = body.getString("correlationId");
    String fallbackCRSString = body.getString("fallbackCRSString");
    // get tags
    JsonArray tagsArr = body.getJsonArray("tags");
    List<String> tags = tagsArr != null ? tagsArr.stream().flatMap(o -> o != null ? Stream.of(o.toString()) : Stream.of()).collect(Collectors.toList()) : null;
    // get properties
    JsonObject propertiesObj = body.getJsonObject("properties");
    Map<String, Object> properties = propertiesObj != null ? propertiesObj.getMap() : null;
    // generate timestamp for this import
    long timestamp = System.currentTimeMillis();
    onImportingStarted(correlationId, filepath, contentType, layer, tagsArr, timestamp);
    FileSystem fs = vertx.fileSystem();
    OpenOptions openOptions = new OpenOptions().setCreate(false).setWrite(false);
    return fs.rxOpen(filepath, openOptions).flatMap(f -> importFile(contentType, f, correlationId, filename, timestamp, layer, tags, properties, fallbackCRSString).doAfterTerminate(() -> {
        // delete file from 'incoming' folder
        log.info("Deleting " + filepath + " from incoming folder");
        f.rxClose().flatMap(v -> fs.rxDelete(filepath)).subscribe(v -> {
        }, err -> {
            log.error("Could not delete file from 'incoming' folder", err);
        });
    })).doOnSuccess(chunkCount -> {
        onImportingFinished(correlationId, filepath, contentType, layer, chunkCount, System.currentTimeMillis() - timestamp, null);
    }).doOnError(err -> {
        onImportingFinished(correlationId, filepath, contentType, layer, null, System.currentTimeMillis() - timestamp, err);
    }).map(count -> null);
}
Also used : JsonArray(io.vertx.core.json.JsonArray) OpenOptions(io.vertx.core.file.OpenOptions) Buffer(io.vertx.rxjava.core.buffer.Buffer) IndexMeta(io.georocket.storage.IndexMeta) StringWindow(io.georocket.util.StringWindow) XMLParserOperator(io.georocket.util.XMLParserOperator) ChunkMeta(io.georocket.storage.ChunkMeta) Window(io.georocket.util.Window) LoggerFactory(io.vertx.core.logging.LoggerFactory) RxStore(io.georocket.storage.RxStore) AbstractVerticle(io.vertx.rxjava.core.AbstractVerticle) Single(rx.Single) JsonParserOperator(io.georocket.util.JsonParserOperator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Result(io.georocket.input.Splitter.Result) Map(java.util.Map) FileSystem(io.vertx.rxjava.core.file.FileSystem) JsonObject(io.vertx.core.json.JsonObject) StoreFactory(io.georocket.storage.StoreFactory) UTF8BomFilter(io.georocket.util.UTF8BomFilter) Logger(io.vertx.core.logging.Logger) Message(io.vertx.rxjava.core.eventbus.Message) XMLSplitter(io.georocket.input.xml.XMLSplitter) OpenOptions(io.vertx.core.file.OpenOptions) GeoJsonSplitter(io.georocket.input.geojson.GeoJsonSplitter) NoStackTraceThrowable(io.vertx.core.impl.NoStackTraceThrowable) FirstLevelSplitter(io.georocket.input.xml.FirstLevelSplitter) Collectors(java.util.stream.Collectors) XMLCRSIndexer(io.georocket.index.xml.XMLCRSIndexer) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Stream(java.util.stream.Stream) MimeTypeUtils.belongsTo(io.georocket.util.MimeTypeUtils.belongsTo) ReadStream(io.vertx.rxjava.core.streams.ReadStream) AddressConstants(io.georocket.constants.AddressConstants) RxUtils(io.georocket.util.RxUtils) ConfigConstants(io.georocket.constants.ConfigConstants) FileSystem(io.vertx.rxjava.core.file.FileSystem) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

Message (io.vertx.rxjava.core.eventbus.Message)4 AddressConstants (io.georocket.constants.AddressConstants)3 ConfigConstants (io.georocket.constants.ConfigConstants)3 ChunkMeta (io.georocket.storage.ChunkMeta)3 IndexMeta (io.georocket.storage.IndexMeta)3 RxStore (io.georocket.storage.RxStore)3 StoreFactory (io.georocket.storage.StoreFactory)3 JsonParserOperator (io.georocket.util.JsonParserOperator)3 MimeTypeUtils.belongsTo (io.georocket.util.MimeTypeUtils.belongsTo)3 RxUtils (io.georocket.util.RxUtils)3 XMLParserOperator (io.georocket.util.XMLParserOperator)3 NoStackTraceThrowable (io.vertx.core.impl.NoStackTraceThrowable)3 JsonArray (io.vertx.core.json.JsonArray)3 JsonObject (io.vertx.core.json.JsonObject)3 Logger (io.vertx.core.logging.Logger)3 LoggerFactory (io.vertx.core.logging.LoggerFactory)3 AbstractVerticle (io.vertx.rxjava.core.AbstractVerticle)3 List (java.util.List)3 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3