The compulsive traders
Let’s review the previous scenarios to have a better view of the current status. In the quote-generator we have build a verticle sending market data to the event bus. In the portfolio service, we expose an event bus service to manage our portfolio. Traders are the missing link between both. It implement the logic deciding when to buy and sell stocks. Traders have only one goal: getting rich (or not…)!
In this section, we are going to develop 3 traders (following a stupid logic you can definitely improve):
The first trader is developed with the raw Vert.x API (callbacks)
The second trader is developer with RX Java 2 (reactive programming)
The third trader is developed with Kotlin using co-routine
1. Initialize katacoda environment
You may have noticed a script running in the terminal. This is getting the lab ready up to this scenario i.e. it is
- Cloning the source code
- Initialize the OpenShift environment
- Build and deploy the quote-generator scenario
- Build and deploy the portfolio-service sceanrio
- Build and deploy the micro-trader-dashboard
2. Compulsive and dumb traders
Before seeing how these are implemented, let’s explain the absolutely illogic algorithm used by these traders:
A compulsive trader is choosing one company name and a number of shares (x)
Randomly, it tries to buy or sell x shares of the company
It does not check whether or not it has enough shares or money, it just tries… This logic is implemented in
MicroTrader Part 3: Compulsive Traders
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));