Java Code Examples for io.vertx.servicediscovery.types.MessageSource#getConsumer()

The following examples show how to use io.vertx.servicediscovery.types.MessageSource#getConsumer() . 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: RawOrderDispatcher.java    From vertx-blueprint-microservice with Apache License 2.0 6 votes vote down vote up
@Override
public void start(Future<Void> future) throws Exception {
  super.start();
  MessageSource.<JsonObject>getConsumer(discovery,
    new JsonObject().put("name", "shopping-order-message-source"),
    ar -> {
      if (ar.succeeded()) {
        MessageConsumer<JsonObject> orderConsumer = ar.result();
        orderConsumer.handler(message -> {
          Order wrappedOrder = wrapRawOrder(message.body());
          dispatchOrder(wrappedOrder, message);
        });
        future.complete();
      } else {
        future.fail(ar.cause());
      }
    });
}
 
Example 2
Source File: CallbackTraderVerticle.java    From vertx-kubernetes-workshop with Apache License 2.0 5 votes vote down vote up
private Future<MessageConsumer<JsonObject>> getMarketSource(ServiceDiscovery discovery) {
    Future<MessageConsumer<JsonObject>> future = Future.future();
    MessageSource.getConsumer(discovery,
        rec -> rec.getName().equalsIgnoreCase("market-data"),
        future);
    return future;
}
 
Example 3
Source File: CallbackTraderVerticle.java    From vertx-kubernetes-workshop with Apache License 2.0 5 votes vote down vote up
private Future<MessageConsumer<JsonObject>> getMarketSource(ServiceDiscovery discovery) {
    Future<MessageConsumer<JsonObject>> future = Future.future();
    MessageSource.getConsumer(discovery,
        rec -> rec.getName().equalsIgnoreCase("market-data"),
        future);
    return future;
}
 
Example 4
Source File: AuditVerticle.java    From microtrader with MIT License 5 votes vote down vote up
private Future<MessageConsumer<JsonObject>> retrieveThePortfolioMessageSource() {
    Future<MessageConsumer<JsonObject>> future = Future.future();
    MessageSource.getConsumer(discovery,
            new JsonObject().put("name", "portfolio-events"),
            future.completer()
    );
    return future;
}
 
Example 5
Source File: MessageSourceExamples.java    From vertx-service-discovery with Apache License 2.0 5 votes vote down vote up
public void example3(ServiceDiscovery discovery) {
  MessageSource.<JsonObject>getConsumer(discovery, new JsonObject().put("name", "some-message-source-service"), ar -> {
    if (ar.succeeded()) {
      MessageConsumer<JsonObject> consumer = ar.result();

      // Attach a message handler on it
      consumer.handler(message -> {
        // message handler
        JsonObject payload = message.body();
      });
      // ...
    }
  });
}
 
Example 6
Source File: JavaCompulsiveTraderVerticle.java    From vertx-microservices-workshop with Apache License 2.0 5 votes vote down vote up
@Override
public void start(Future<Void> future) {
  super.start();

  //----
  // Initialize the trader
  String company = TraderUtils.pickACompany();
  int numberOfShares = TraderUtils.pickANumber();
  System.out.println("Java compulsive trader configured for company " + company + " and shares: " + numberOfShares);

  // We need to retrieve two services, create two futures object that will get the services
  Future<MessageConsumer<JsonObject>> marketFuture = Future.future();
  Future<PortfolioService> portfolioFuture = Future.future();
  // Retrieve the services, use the "special" completed to assign the future
  MessageSource.getConsumer(discovery, new JsonObject().put("name", "market-data"), marketFuture);
  EventBusService.getProxy(discovery, PortfolioService.class, portfolioFuture);

  // When done (both services retrieved), execute the handler
  CompositeFuture.all(marketFuture, portfolioFuture).setHandler(ar -> {
    if (ar.failed()) {
      future.fail("One of the required service cannot " +
          "be retrieved: " + ar.cause());
    } else {
      // Our services:
      PortfolioService portfolio = portfolioFuture.result();
      MessageConsumer<JsonObject> marketConsumer = marketFuture.result();

      // Listen the market...
      marketConsumer.handler(message -> {
        JsonObject quote = message.body();
        TraderUtils.dumbTradingLogic(company, numberOfShares, portfolio, quote);
      });

      future.complete();
    }
  });
  // ----
}