Java Code Examples for io.vertx.servicediscovery.types.EventBusService#getProxy()

The following examples show how to use io.vertx.servicediscovery.types.EventBusService#getProxy() . 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: APIGatewayVerticle.java    From vertx-blueprint-microservice with Apache License 2.0 6 votes vote down vote up
private void authUaaHandler(RoutingContext context) {
  if (context.user() != null) {
    JsonObject principal = context.user().principal();
    String username = null;  // TODO: Only for demo. Complete this in next version.
    // String username = KeycloakHelper.preferredUsername(principal);
    if (username == null) {
      context.response()
        .putHeader("content-type", "application/json")
        .end(new Account().setId("TEST666").setUsername("Eric").toString()); // TODO: no username should be an error
    } else {
      Future<AccountService> future = Future.future();
      EventBusService.getProxy(discovery, AccountService.class, future.completer());
      future.compose(accountService -> {
        Future<Account> accountFuture = Future.future();
        accountService.retrieveByUsername(username, accountFuture.completer());
        return accountFuture.map(a -> {
          ServiceDiscovery.releaseServiceObject(discovery, accountService);
          return a;
        });
      })
        .setHandler(resultHandlerNonEmpty(context)); // if user does not exist, should return 404
    }
  } else {
    context.fail(401);
  }
}
 
Example 2
Source File: CompulsiveTraderVerticle.java    From microtrader with MIT License 5 votes vote down vote up
@Override
public void start(Future<Void> future) {
    super.start();

    // Get configuration
    config = ConfigFactory.load();

    String company = TraderUtils.pickACompany();
    int numberOfShares = TraderUtils.pickANumber();

    EventBus eventBus = vertx.eventBus();
    EventBusService.getProxy(discovery, PortfolioService.class, ar -> {
        if (ar.failed()) {
            System.out.println("Portfolio service could not be retrieved: " + ar.cause());
        } else {
            // Our services:
            PortfolioService portfolio = ar.result();
            MessageConsumer<JsonObject> marketConsumer = eventBus.consumer(config.getString("market.address"));

            // Listen to the market...
            marketConsumer.handler(message -> {
                JsonObject quote = message.body();
                TraderUtils.dumbTradingLogic(company, numberOfShares, portfolio, quote);
            });
        }
    });
}
 
Example 3
Source File: EventBusServiceJavaExamples.java    From vertx-service-discovery with Apache License 2.0 5 votes vote down vote up
public void example3(ServiceDiscovery discovery) {
  EventBusService.getProxy(discovery, MyService.class, ar -> {
    if (ar.succeeded()) {
      MyService service = ar.result();

      // Dont' forget to release the service
      ServiceDiscovery.releaseServiceObject(discovery, service);
    }
  });
}
 
Example 4
Source File: EventBusServiceExamples.java    From vertx-service-discovery with Apache License 2.0 5 votes vote down vote up
public void example3(ServiceDiscovery discovery) {
  EventBusService.getProxy(discovery, MyService.class, ar -> {
    if (ar.succeeded()) {
      MyService service = ar.result();

      // Dont' forget to release the service
      ServiceDiscovery.releaseServiceObject(discovery, service);
    }
  });
}
 
Example 5
Source File: CheckoutServiceImpl.java    From vertx-blueprint-microservice with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch global counter of order from the cache infrastructure.
 *
 * @param key counter key (type)
 * @return async result of the counter
 */
private Future<Long> retrieveCounter(String key) {
  Future<Long> future = Future.future();
  EventBusService.getProxy(discovery, CounterService.class,
    ar -> {
      if (ar.succeeded()) {
        CounterService service = ar.result();
        service.addThenRetrieve(key, future.completer());
      } else {
        future.fail(ar.cause());
      }
    });
  return future;
}
 
Example 6
Source File: CheckoutServiceImpl.java    From vertx-blueprint-microservice with Apache License 2.0 5 votes vote down vote up
private Future<ShoppingCart> getCurrentCart(String userId) {
  Future<ShoppingCartService> future = Future.future();
  EventBusService.getProxy(discovery, ShoppingCartService.class, future.completer());
  return future.compose(service -> {
    Future<ShoppingCart> cartFuture = Future.future();
    service.getShoppingCart(userId, cartFuture.completer());
    return cartFuture.compose(c -> {
      if (c == null || c.isEmpty())
        return Future.failedFuture(new IllegalStateException("Invalid shopping cart"));
      else
        return Future.succeededFuture(c);
    });
  });
}
 
Example 7
Source File: CheckoutServiceImpl.java    From vertx-blueprint-microservice with Apache License 2.0 5 votes vote down vote up
/**
 * Save checkout cart event for current user.
 *
 * @param userId user id
 * @return async result
 */
private Future<Void> saveCheckoutEvent(String userId) {
  Future<ShoppingCartService> future = Future.future();
  EventBusService.getProxy(discovery, ShoppingCartService.class, future.completer());
  return future.compose(service -> {
    Future<Void> resFuture = Future.future();
    CartEvent event = CartEvent.createCheckoutEvent(userId);
    service.addCartEvent(event, resFuture.completer());
    return resFuture;
  });
}
 
Example 8
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();
    }
  });
  // ----
}
 
Example 9
Source File: ShoppingCartServiceImpl.java    From vertx-blueprint-microservice with Apache License 2.0 4 votes vote down vote up
/**
 * Get product service from the service discovery infrastructure.
 *
 * @return async result of the service.
 */
private Future<ProductService> getProductService() {
  Future<ProductService> future = Future.future();
  EventBusService.getProxy(discovery, ProductService.class, future.completer());
  return future;
}