Tutorial: Reactive Spring Boot Part 7 – Subscribing Multiple Consumers

This is the seventh part of our tutorial showing how to build a Reactive application using Spring Boot, Kotlin, Java and JavaFX.  The original inspiration was a 70 minute live demo.

In this lesson we update our live-updating chart to show prices for more than one stock, which means subscribing more than one consumer to our reactive stream of prices.

This blog post contains a video showing the process step-by-step and a textual walk-through (adapted from the transcript of the video) for those who prefer a written format.

This tutorial is a series of steps during which we will build a full Spring Boot application featuring a Kotlin back end, a Java client and a JavaFX user interface.

In the last step we made our JavaFX line chart subscribe to prices from our Reactive Spring Boot service and display them in real time. In this step, we’re going to update the chart so that it subscribes multiple times and can show multiple sets of prices on the same chart.

Introducing a price subscriber

The ChartController, which manages how the data gets to the view, contains a call that subscribes the ChartController itself to the client that listens to the price service (look at the previous post to see the starting state of ChartController for this lesson).  We need to change this so we can have more than one subscriber.

  1. Inside the subscribe call in ChartController, replace this with a call to the constructor of a new type, PriceSubscriber.  This class will manage everything to do with consuming prices from the client.
  2. Create PriceSubscriber as an inner class, it needs to implement Consumer and consume StockPrice.
  3. (Tip: IntelliJ IDEA will offer to automatically create this for us if we press Alt+Enter on the PriceSubscriber name after we first type it.)

Moving responsibilities to the PriceSubscriber

  1. Move the accept method from the ChartController and into the PriceSubscriber.
  2. Remove the “implements Consumer” declaration from the ChartController.
  3. Move seriesData from the ChartController into PriceSubscriber.
  4. Call the PriceSubscriber constructor with the symbol, and update the PriceSubscriber to accept this as a constructor parameter.
  5. (Tip: We can get IntelliJ IDEA to create the appropriate constructor if we pass symbol in, press Alt+Enter and select “Create constructor).
  6. Move the creation of the Series into the PriceSubscriber constructor and store the series as a field.
  7. Add a getter for series because the ChartController will need to get this series to add it to the chart.
  8. (Tip: IntelliJ IDEA can generate getters from fields.)

Let’s fix up the initialize method of ChartController.

  1. Extract the new PriceSubscriber into a local variable, we’re going to need to use this elsewhere in the method.
  2. Move this priceSubscriber near the top of the method, and inside data.add call the getter.

If we re-run the application, the chart should run the same way as it did before (assuming we still have the back-end service running as well), since we haven’t changed the behaviour just refactored the subscription code.

Adding a second subscriber

  1. Rename symbol to symbol1, since we’re going to have another one of these, rename the symbol itself, and let’s also rename our priceSubscriber as well.
  2. (Tip: Use the rename refactoring, Shift+F6, to do this, it makes sure all the code still compiles).
  3. Duplicate these lines and change the variable names to symbol2 and priceSubscriber2.
  4. (Tip: The keyboard shortcut for Duplicate Line is Ctrl+D/⌘D.)
  5. Add the second series to the chart to display this second set of prices.
  6. Duplicate the subscribe code and pass in the second symbol and second subscriber.

Now when we re-run the application we can see two different series on the chart tracking the prices for two different stocks, like you can see at 3:30 in the video.

Code clean up

Now we have the application working the way we want, we can refactor the code if we wish.

  1. We could move the call to subscribe to the top near the where we create the subscriber.
  2. We can clean up any warnings, for example making the inner class static.

Bonus Refactoring (not in the video!)

There are a number of other ways we could further refactor this to make it easier to read, to reduce duplication, or to group responsibilities differently.  For example, we could move the call to subscribe into the PriceSubscriber to reduce duplication. We can even use var if we’re running a version of Java that supports it.

Conclusion

So now we have a JavaFX application that subscribes to more than one price from our Spring Boot service, and displays each set of price data in real time in multiple series on a line chart.

Full code is available on GitHub.

This entry was posted in Tutorials and tagged , , , , . Bookmark the permalink.

2 Responses to Tutorial: Reactive Spring Boot Part 7 – Subscribing Multiple Consumers

  1. Wim Deblauwe says:

    It would be interesting to know how it could be implemented that users can type a stock symbol in a text field in the JavaFX UI and then update the subscribers. It is not clear to me how that can be done.

    • Trisha Gee says:

      That’s a good idea. I have an example that does something similar from an earlier demo, I could dig it out and add it to the tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *