public static <T> Single<T> rxGetProxy(ServiceDiscovery discovery, Class<T> clientClass, Function<Record, Boolean> filter) { return new io.vertx.reactivex.core.impl.AsyncResultSingle<>(handler -> EventBusService.getServiceProxy(discovery, filter, clientClass, handler) ); } }
private void delegateWithCircuitBreaker(RoutingContext rc) { HttpEndpoint.rxGetWebClient(discovery, svc -> svc.getName().equals("currency-3rdparty-service")) .flatMap(client -> { // TODO // Use the circuit breaker (circuit) to call the service. Use the rxExecuteCommandWithFallback` method. // This methods takes 2 parameters: the first one if a function taking a `Future` as parameter and // needs to report the success or failure on this future. The second method is a function providing // the fallback result. You must provide a JSON object as response. For the fallback use: // new JsonObject() // .put("amount", rc.getBodyAsJson().getDouble("amount")) // .put("currency", "USD")) // In the first function, use the given client, emit a POST request on / containing the incoming // payload (rc.getBodyAsJson()). Extract the response payload as JSON (bodyAsJsonObject). Don't // forget to subscribe (you can use subscribe(toObserver(fut)). You can have a look to the `delegate` // method as example. // ----- return Single.just(new JsonObject().put("amount", 0.0).put("currency", "N/A")); }) // ---- .map(JsonObject::toBuffer) .map(Buffer::new) .subscribe(toObserver(rc)); }
@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(); //---- }); }
HttpEndpoint.getClient(discovery, record -> record.getName().equalsIgnoreCase("my-http-service"), ar -> { if (ar.failed()) { HttpEndpoint.getWebClient(discovery, record -> record.getName().equalsIgnoreCase("my-http-service"), ar -> { if (ar.failed()) { EventBusService.getServiceProxy(discovery, record -> record.getName().equalsIgnoreCase("my-service"), HelloService.class, JDBCDataSource.getJDBCClient(discovery, record -> record.getName().equalsIgnoreCase("my-data-source"), ar -> { if (ar.failed()) { RedisDataSource.getRedisClient(discovery, record -> record.getName().equalsIgnoreCase("my-redis-data-source"), ar -> { if (ar.failed()) { MongoDataSource.getMongoClient(discovery, record -> record.getName().equalsIgnoreCase("my-mongo-data-source"), ar -> { MessageSource.getConsumer(discovery, record -> record.getName().equalsIgnoreCase("my-message-source-1"), ar -> { if (ar.failed()) {
Single<JDBCClient> jdbc = JDBCDataSource.rxGetJDBCClient(discovery, svc -> svc.getName().equals("audit-database"), getDatabaseConfiguration()
@Override public void start() { ServiceDiscovery.create(vertx, discovery -> { this.discovery = discovery; // Create the service object PortfolioServiceImpl service = new PortfolioServiceImpl(vertx, discovery, config().getDouble("money", 10000.00)); // Register the service proxy on the event bus ProxyHelper.registerService(PortfolioService.class, vertx.getDelegate(), service, ADDRESS); Record record = EventBusService.createRecord("portfolio", ADDRESS, PortfolioService.class.getName()); discovery.publish(record, ar -> { if (ar.succeeded()) { this.record = record; System.out.println("Portfolio service published"); // Used for health check vertx.createHttpServer().requestHandler(req -> req.response().end("OK")).listen(8080); } else { ar.cause().printStackTrace(); } }); }); }
Path path = klass.getAnnotation(Path.class); if(serviceName != null) { Record record = HttpEndpoint.createRecord(serviceName.value(), host, port, path.value()); records = records.andThen(discovery.rxPublish(record).ignoreElement()); methodPathString += methodPath.value(); Record record = HttpEndpoint.createRecord(methodServiceName.value(), host, port, methodPathString); records = records.andThen(discovery.rxPublish(record).ignoreElement());
/** * This method is called when the verticle is deployed. */ @Override public void start(Future<Void> future) { discovery = ServiceDiscovery.create(vertx); ConfigRetriever retriever = ConfigRetriever.create(vertx, getConfigurationOptions()); retriever.rxGetConfig() // Read the configuration, and deploy a MarketDataVerticle for each company listed in the configuration. .flatMap(config -> Observable.fromIterable(config.getJsonArray("companies")) .cast(JsonObject.class) // Deploy the verticle with a configuration. .flatMapSingle(company -> vertx.rxDeployVerticle(MarketDataVerticle.class.getName(), new DeploymentOptions().setConfig(company))) .toList() ) // Deploy another verticle .flatMap(l -> vertx.rxDeployVerticle(RestQuoteAPIVerticle.class.getName())) // Expose the market-data message source .flatMap(x -> discovery.rxPublish(MessageSource.createRecord("market-data", ADDRESS))) .subscribe((rec, err) -> { if (rec != null) { this.record = rec; future.complete(); } else { future.fail(err); } }); }
/** * Method to check the proxy requesting to convert the current portfolio to EUR. * * @param rc the routing context */ private void convertPortfolioToEuro(RoutingContext rc) { EventBusService.getServiceProxy(discovery, svc -> svc.getName().equals("portfolio"), PortfolioService.class, ar -> { if (ar.failed()) { rc.fail(ar.cause()); } else { ar.result().evaluate(res -> { if (res.failed()) { rc.fail(res.cause()); } else { JsonObject payload = new JsonObject().put("amount", res.result()).put("currency", "EUR"); rc.setBody(new Buffer(payload.toBuffer())); delegateWithCircuitBreaker(rc); } }); } }); } }
/** * Example of method not using a circuit breaker. * * @param rc the routing context */ private void delegate(RoutingContext rc) { HttpEndpoint.rxGetWebClient(discovery, svc -> svc.getName().equals("currency-3rdparty-service")) .flatMap(client -> client.post("/").rxSendJsonObject(rc.getBodyAsJson())) .map(HttpResponse::bodyAsBuffer) .subscribe(toObserver(rc)); }