io.vertx.reactivex.core.eventbus.Message Java Examples

The following examples show how to use io.vertx.reactivex.core.eventbus.Message. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DataProcessor.java    From smallrye-reactive-streams-operators with Apache License 2.0 5 votes vote down vote up
@Override
public void start(Future<Void> done) {
    vertx.createHttpServer()
            .requestHandler(request -> {
                // Consume messages from the Vert.x event bus
                MessageConsumer<JsonObject> consumer = vertx.eventBus().consumer("data");
                // Wrap the stream and manipulate the data
                ReactiveStreams.fromPublisher(consumer.toFlowable())
                        .limit(5) // Take only 5 messages
                        .map(Message::body) // Extract the body
                        .map(json -> json.getInteger("value")) // Extract the value
                        .peek(i -> System.out.println("Got value: " + i)) // Print it
                        .reduce(0, (acc, value) -> acc + value)
                        .run() // Begin to receive items
                        .whenComplete((res, err) -> {
                            // When the 5 items has been consumed, write the result to the
                            // HTTP response:
                            if (err != null) {
                                request.response().setStatusCode(500).end(err.getMessage());
                            } else {
                                request.response().end("Result is: " + res);
                            }
                        });
            })
            .listen(PORT, ar -> done.handle(ar.mapEmpty()));

}
 
Example #2
Source File: VertxChannel.java    From ocraft-s2client with MIT License 5 votes vote down vote up
private Observable<byte[]> initOutputStream(EventBus eventBus) {
    log.debug("registering event bus consumer [{}] for output stream", OUTPUT_STREAM);
    return eventBus
            .<byte[]>consumer(OUTPUT_STREAM)
            .toObservable()
            .map(Message::body)
            .doOnComplete(errorStream::onComplete);
}
 
Example #3
Source File: EventBusReporterWrapper.java    From graviteeio-access-management with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(Message<Reportable> reportableMsg) {
    Reportable reportable = reportableMsg.body();

    if (canHandle(reportable)) {
        reporter.report(reportable);
    }
}
 
Example #4
Source File: RxifiedExamples.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
public void eventBusMessages(Vertx vertx) {
  EventBus eb = vertx.eventBus();
  MessageConsumer<String> consumer = eb.<String>consumer("the-address");
  Observable<Message<String>> observable = consumer.toObservable();
  Disposable sub = observable.subscribe(msg -> {
    // Got message
  });

  // Unregisters the stream after 10 seconds
  vertx.setTimer(10000, id -> {
    sub.dispose();
  });
}
 
Example #5
Source File: VertxChannel.java    From ocraft-s2client with MIT License 4 votes vote down vote up
private Observable<byte[]> initInputStream(EventBus eventBus) {
    log.debug("registering event bus consumer [{}] for input stream", INPUT_STREAM);
    return eventBus.<byte[]>consumer(INPUT_STREAM).toObservable().map(Message::body);
}
 
Example #6
Source File: RestQuoteAPIVerticle.java    From vertx-kubernetes-workshop with Apache License 2.0 4 votes vote down vote up
@Override
public void start(Future<Void> future) throws Exception {
    // Get the stream of messages sent on the "market" address
    vertx.eventBus().<JsonObject>consumer(GeneratorConfigVerticle.ADDRESS).toFlowable()
        // TODO Extract the body of the message using `.map(msg -> {})`
        // ----
        .map(Message::body)
        // ----
        // TODO For each message, populate the `quotes` map with the received quote. Use `.doOnNext(json -> {})`
        // Quotes are json objects you can retrieve from the message body
        // The map is structured as follows: name -> quote
        // ----
        .doOnNext(json -> {
            quotes.put(json.getString("name"), json); // 2
        })
        // ----
        .subscribe();

    HttpServer server = vertx.createHttpServer();
    server.requestStream().toFlowable()
        .doOnNext(request -> {
            HttpServerResponse response = request.response()
                .putHeader("content-type", "application/json");

            // TODO Handle the HTTP request
            // The request handler returns a specific quote if the `name` parameter is set, or the whole map if none.
            // To write the response use: `response.end(content)`
            // If the name is set but not found, you should return 404 (use response.setStatusCode(404)).
            // To encode a Json object, use the `encorePrettily` method
            // ----
            
            String company = request.getParam("name");
            if (company == null) {
                String content = Json.encodePrettily(quotes);
                response.end(content);
            } else {
                JsonObject quote = quotes.get(company);
                if (quote == null) {
                    response.setStatusCode(404).end();
                } else {
                    response.end(quote.encodePrettily());
                }
            }
            // ----
        })
        .subscribe();

    server.rxListen(config().getInteger("http.port", 8080))
        .toCompletable()
        .subscribe(CompletableHelper.toObserver(future));
}