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

The following examples show how to use io.vertx.reactivex.core.eventbus.MessageConsumer. 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: RXCompulsiveTraderVerticle.java    From vertx-kubernetes-workshop with Apache License 2.0 5 votes vote down vote up
@Override
public void start(Future<Void> future) {
    String company = TraderUtils.pickACompany();
    int numberOfShares = TraderUtils.pickANumber();
    System.out.println("Java-RX compulsive trader configured for company " + company + " and shares: " +
        numberOfShares);


    ServiceDiscovery.create(vertx, discovery -> {
        Single<PortfolioService> retrieveThePortfolioService = RXEventBusService.rxGetProxy(discovery, PortfolioService.class,
            rec -> rec.getName().equalsIgnoreCase("portfolio"));

        Single<MessageConsumer<JsonObject>> retrieveTheMarket = MessageSource.rxGetConsumer(discovery,
            rec -> rec.getName().equalsIgnoreCase("market-data"));

        //TODO
        //----

        // TODO 1 - "wait" for both single to be completed (using Single.zip or Single.zipWith methods)

        // TODO 2 - When both single have completed, attach the handler to the message consumer to execute the
        // trading logic

        // TODO 3 - Use the TraderUtils.drumpTradingLogic method returning a Completable. Don't forget to
        // subscribe to it, or nothing will happen. Return "true" to comply with the "zip" operator
        // signature.


        // TODO 4 - Transform the output into a Completable (toCompletable) and subscribe to it using:
        //.subscribe(CompletableHelper.toObserver(future)) - it reports the failure or success to the `done`
        // future.
        
        // To remove
        future.complete();
        //----

    });
}
 
Example #3
Source File: AuditVerticle.java    From vertx-kubernetes-workshop with Apache License 2.0 5 votes vote down vote up
/**
 * Starts the verticle asynchronously. The the initialization is completed, it calls
 * `complete()` on the given {@link Future} object. If something wrong happens,
 * `fail` is called.
 *
 * @param future the future to indicate the completion
 */
@Override
public void start(Future<Void> future) {
    // creates the jdbc client.

    ServiceDiscovery.create(vertx, discovery -> {

        // Discover and configure the database.
        Single<JDBCClient> jdbc = JDBCDataSource.rxGetJDBCClient(discovery,
            svc -> svc.getName().equals("audit-database"),
            getDatabaseConfiguration()
        ).doOnSuccess(jdbcClient -> this.jdbc = jdbcClient);

        // TODO
        // ----

        Single<MessageConsumer<JsonObject>> readySingle = Single
            .error(new UnsupportedOperationException("Not implemented yet"));

        // ----

        // signal a verticle start failure
        readySingle.doOnSuccess(consumer -> {
            // on success we set the handler that will store message in the database
            consumer.handler(message -> storeInDatabase(message.body()));
        }).subscribe(consumer -> {
            // complete the verticle start with a success
            future.complete();
            // indicate our readiness state
            ready = true;
        }, future::fail);
    });


}
 
Example #4
Source File: AuditVerticle.java    From vertx-kubernetes-workshop with Apache License 2.0 5 votes vote down vote up
/**
 * Starts the verticle asynchronously. The the initialization is completed, it calls
 * `complete()` on the given {@link Future} object. If something wrong happens,
 * `fail` is called.
 *
 * @param future the future to indicate the completion
 */
@Override
public void start(Future<Void> future) {
    // creates the jdbc client.

    ServiceDiscovery.create(vertx, discovery -> {

        // Discover and configure the database.
        Single<JDBCClient> jdbc = JDBCDataSource.rxGetJDBCClient(discovery,
            svc -> svc.getName().equals("audit-database"),
            getDatabaseConfiguration()
        ).doOnSuccess(jdbcClient -> this.jdbc = jdbcClient);

        // TODO
        // ----

        Single<JDBCClient> databaseReady = jdbc
            .flatMap(client -> initializeDatabase(client, true));
        Single<HttpServer> httpServerReady = configureTheHTTPServer();
        Single<MessageConsumer<JsonObject>> messageConsumerReady = retrieveThePortfolioMessageSource();

        Single<MessageConsumer<JsonObject>> readySingle = Single.zip(databaseReady, httpServerReady,
            messageConsumerReady, (db, http, consumer) -> consumer);

        // ----

        // signal a verticle start failure
        readySingle.doOnSuccess(consumer -> {
            // on success we set the handler that will store message in the database
            consumer.handler(message -> storeInDatabase(message.body()));
        }).subscribe(consumer -> {
            // complete the verticle start with a success
            future.complete();
            ready = true;
        }, future::fail);
    });


}
 
Example #5
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 #6
Source File: RXCompulsiveTraderVerticle.java    From vertx-kubernetes-workshop with Apache License 2.0 4 votes vote down vote up
@Override
public void start(Future<Void> future) {
    String company = TraderUtils.pickACompany();
    int numberOfShares = TraderUtils.pickANumber();
    System.out.println("Java-RX compulsive trader configured for company " + company + " and shares: " +
        numberOfShares);


    ServiceDiscovery.create(vertx, discovery -> {
        Single<PortfolioService> retrieveThePortfolioService = RXEventBusService.rxGetProxy(discovery, PortfolioService.class,
            rec -> rec.getName().equalsIgnoreCase("portfolio"));

        Single<MessageConsumer<JsonObject>> retrieveTheMarket = MessageSource.rxGetConsumer(discovery,
            rec -> rec.getName().equalsIgnoreCase("market-data"));

        //TODO
        //----

        // TODO 1 - "wait" for both single to be completed (using Single.zip or Single.zipWith methods)
        retrieveThePortfolioService.zipWith(retrieveTheMarket, (ps, consumer) -> {

        // TODO 2 - When both single have completed, attach the handler to the message consumer to execute the
            // trading logic

            consumer.handler(message ->
                // TODO 3 - Use the TraderUtils.drumpTradingLogic method returning a Completable. Don't forget to
                // subscribe to it, or nothing will happen. Return "true" to comply with the "zip" operator
                // signature.
                
                TraderUtils.dumbTradingLogic(company, numberOfShares, ps, message.body()).subscribe());
            // We need to return something as requested by the "zip" signature.
            return true;
        })

            // TODO 4 - Transform the output into a Completable (toCompletable) and subscribe to it using:
            //.subscribe(CompletableHelper.toObserver(future)) - it reports the failure or success to the `done`
            // future.
            .toCompletable()
            .subscribe(CompletableHelper.toObserver(future));
        //----

    });
}
 
Example #7
Source File: AuditVerticle.java    From vertx-kubernetes-workshop with Apache License 2.0 4 votes vote down vote up
private Single<MessageConsumer<JsonObject>> retrieveThePortfolioMessageSource() {
    // Example of Single returning a single item already known:
    return Single.just(vertx.eventBus().consumer("portfolio"));
}
 
Example #8
Source File: AuditVerticle.java    From vertx-kubernetes-workshop with Apache License 2.0 4 votes vote down vote up
private Single<MessageConsumer<JsonObject>> retrieveThePortfolioMessageSource() {
    // Example of Single returning a single item already known:
    return Single.just(vertx.eventBus().consumer("portfolio"));
}
 
Example #9
Source File: RxifiedExamples.java    From vertx-rx with Apache License 2.0 4 votes vote down vote up
public void eventBusBodies(Vertx vertx) {
  EventBus eb = vertx.eventBus();
  MessageConsumer<String> consumer = eb.<String>consumer("the-address");
  Observable<String> observable = consumer.bodyStream().toObservable();
}