Reactive Spring Boot: Part 7: Subscribing Multiple Consumers

In the last video, we made our JavaFX line
chart subscribe to prices from our Reactive Spring Boot service and display them in real
time. In this video, we’re going to update the chart
so that it subscribes multiple times and can show multiple sets of prices on the same chart. 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. We need to change this so we can have more
than one subscriber. Let’s create a new type, a PriceSubscriber,
which manages everything to do with consuming prices from the client. We’ll create this as an inner class. We know from the last lesson that this accept method is the one that deals with prices,
so let’s move this into the PriceSubscriber and make a small change to get the PriceSubscriber
to compile. We want the PriceSubscriber to manage the
series data, as this is the collection that gets updated by the accept method. We also need the PriceSubscriber to know the
symbol, each subscriber is responsible for managing a single stock. We’ll get IntelliJ IDEA to create the appropriate
constructor, and to create a field for the symbol parameter. Since each stock also corresponds to a single series on the chart, we can also move the
creation of the Series into the PriceSubscriber. Let’s move the series into a field so we can
use it elsewhere in the class. We can make all these fields final since we’re
not going to change them anywhere one they’re initialised. We need to add a Getter for series because
the ChartController will need to get this series to add it to the chart. Let’s extract the PriceSubscriber into a local variable, we’re going to need to use this
elsewhere in the method. To declare this as final, we can hold down
Alt and press the F key. Let’s move this priceSubscriber near the top
of the method, now we can get the series from it to add it to the chart. The ChartController still doesn’t compile because we said it still implements the consumer
interface, which it doesn’t any more, so we need to delete this. 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. And indeed it does all work as before. Now we want to add our second subscriber. Let’s rename our symbol to symbol one, since
we’re going to have another one of these, and let’s also rename our priceSubscriber
as well. We can duplicate these lines and make them
work for a second symbol. The second symbol and price subscriber are needed for a second subscription to the client
that retrieves prices. We also need to add our second series to the
chart to display this second set of prices. Let’s re-run the application to see if this has worked. Now we can see two different series on the
chart tracking the prices for two different stocks. Now we have the application working the way we want, we can refactor the code if we wish. For example, we might want all the code that
sets up the subscription for each symbol in the same section of the method, so we could
move the call to subscribe to the top near the where we create the subscriber. We can clean up any warnings, so here we can make the inner class static as suggested. And although we thought we might need symbol
as a field, it turns out we don’t need it elsewhere in the class so we can convert it
to a local variable. IntelliJ IDEA automatically inlines this since
it’s only used in the call to the Series constructor. 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. Thanks for watching!

One Comment

  • Is there any solution in Spring/Java world to show a realtime chart like this in the web browser?

Leave a Comment

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