Difficulty: beginner
Estimated Time: 15 minutes

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

It gives you an overview of the different programming style proposed by Vert.x, and let you decide which one you prefer. Also notice that Vert.x supports JavaScript, Groovy, Ruby, Scala and Ceylon.

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

  1. Cloning the source code
  2. Initialize the OpenShift environment
  3. Build and deploy the quote-generator scenario
  4. Build and deploy the portfolio-service sceanrio
  5. 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 io.vertx.workshop.trader.impl.TraderUtils.


In this scenario, you covered:

  • how to consume an event bus service

  • how to write a verticle using the plain Vert.x API (callbacks)

  • how to write a verticle using RX Java 2

Eclipse Vert.x on RHOAR

Step 1 of 3

Step 1

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. A Future is a structure encapsulating a deferred result (or failure). Unlike regular Java Future, Vert.x Futures are non-blocking (no get and 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> using the Future.setHandler method.

Open the 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));
Local Web Browser
OpenShift Console