The callback-based trader
Let’s start with the callback trader. This trader is developed using the raw Vert.x API and so callbacks. To ease the readability of the code, we are going to use Vert.x
Future is a structure encapsulating a deferred result (or failure). Unlike regular Java Future, Vert.x
Futures are non-blocking (no
join methods). You needs to listen for the
Future completion by attaching a
Handler indicating the result (success or failure).
Futures provide a nice and simple way to structure your code, but that’s not all. It also provides high-level operators such as
CompositeFuture.all indicating the completion of a set of Futures.
Future objects are created using Future.future(). To be notified of the completion, you need to attach a Handler
io.vertx.workshop.trader.impl.CallbackTraderVerticle class and fill in the code to complete TODOs 1 and 2.
The trader needs the Portfolio service and the market service (the message source sending the market data). We cannot start the trading logic before having retrieved both of them. Use a CompositeFuture to be notified when both are completed (or one fails). A CompositeFuture is a Future, so attach a Handler to it that call the initialize method.
Future<PortfolioService> retrieveThePortfolioService = getPortfolioService(discovery.result()); Future<MessageConsumer<JsonObject>> retrieveTheMarket = getMarketSource(discovery.result());
CompositeFuture.all(retrieveServiceDiscovery, retrieveTheMarket) .setHandler(x -> initialize(done, company, numberOfShares, retrieveThePortfolioService, retrieveTheMarket, x));