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