public <T> void getProxy(Class<T> serviceClass, Handler<AsyncResult<T>> handler) { EventBusService.getProxy(serviceDiscovery, serviceClass, handler); }
/** * 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; } }
/** * 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; }
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); }); }); }
/** * 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; }); }
@Test public void testUsingGetMethod() { HelloService svc = new HelloServiceImpl("stuff"); ProxyHelper.registerService(HelloService.class, vertx, svc, "address"); Record record = EventBusService.createRecord("Hello", "address", HelloService.class); discovery.publish(record, (r) -> { }); await().until(() -> record.getRegistration() != null); AtomicReference<HelloService> found = new AtomicReference<>(); EventBusService.getProxy(discovery, HelloService.class, ar -> { found.set(ar.result()); }); await().until(() -> found.get() != null); Assertions.assertThat(discovery.bindings()).hasSize(1); HelloService hello = found.get(); AtomicReference<String> result = new AtomicReference<>(); hello.hello(name, ar -> result.set(ar.result())); await().untilAtomic(result, not(nullValue())); ServiceDiscovery.releaseServiceObject(discovery, found.get()); Assertions.assertThat(discovery.bindings()).hasSize(0); }
@Test public void testUsingGetMethod() { HelloService svc = new HelloServiceImpl("stuff"); ProxyHelper.registerService(HelloService.class, vertx, svc, "address"); Record record = EventBusService.createRecord("Hello", "address", HelloService.class); discovery.publish(record, (r) -> { }); await().until(() -> record.getRegistration() != null); AtomicReference<HelloService> found = new AtomicReference<>(); EventBusService.getProxy(discovery, HelloService.class, ar -> { found.set(ar.result()); }); await().until(() -> found.get() != null); Assertions.assertThat(discovery.bindings()).hasSize(1); HelloService hello = found.get(); AtomicReference<String> result = new AtomicReference<>(); hello.hello(name, ar -> result.set(ar.result())); await().untilAtomic(result, not(nullValue())); ServiceDiscovery.releaseServiceObject(discovery, found.get()); Assertions.assertThat(discovery.bindings()).hasSize(0); }
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); } }