Reactive Spring Boot: Part 10: Profiles to Switch Clients


Now we have an Rsocket client that lets us connect to our Rsocket server, we want to use this from our JavaFX application. We intentionally have two implementations of our StockClient, one for connecting via RSOcket and one via WebClient. Our ClientConfiguration only exposes one of
these, the WebClientStockClient, as a bean. If we want applications to be able to use
the Rsocket client we need to add an Rsocket client bean as well. This second bean can also be of type StockClient,
let’s call the method rSocketStockClient to make it clear what we’re creating. We’ll create a new RSocketStockClient and
pass in an rSocketRequester, we can add this as a parameter for this method. IntelliJ IDEA is telling us we don’t have
any beans of this type either, so let’s create a Bean method that returns an RSocketRequester,
called rSocketRequester. Remember from our test that we can use a builder
to create the RSocketRequester, we’ll pass this in as a method parameter, the type is
RSocketRequester.Builder like we used in our test. We can use connectTcp and give it localhost
and port 7000 because that’s where our Spring Boot Rsocket server is running. We need to call block to complete this connection. Our stock-client project is actually made
of two modules; this stock-client module which contains the code that connects to the servers,
and the stock-ui module that contains the JavaFX user interface code. It’s the JavaFX ChartController that uses
the StockClient to connect to the price service and display prices on the chart. IntelliJ IDEA is now giving us a warning in
this class that there’s more than one Bean that matches the StockClient type, our RSocket
stock client and our WebClient stock client. We need to figure out a way to specify which
client we really want to use. One way to do this is with Spring profiles. We can give each of these beans a different
profile, for example our WebClientStockClient could have a profile of sse, for server sent
events. We’ll give the RSocketStockClient a profile
of “rsocket”. When we go to the ChartController, we can
see the error has gone away. But we still need to say which profile we
want to use. We can do this by going to application.properties
and setting the spring.profiles.active property to whichever profile we want. Let’s start with sse, because this should
give us the same bean and the same functionality that we had before. When we re-run the application, we can see
the application starts up as expected and the chart shows two sets of prices as before. We can also see that the JavaFX application
has started up with the sse profile. If we want to be extra sure that we’re using
the bean we think we’re using, we could go back to the WebClientStockClient and add some
logging. Let’s make it clear from the message which
client we’re using. Now, when we re-run the application, we see
that we are definitely using the WebClient stock client. Let’s go to the RSocketStockClient and add
similar logging so we have the same kind of visibility. Now let’s go back to the JavaFX application.properties
and change the active profile to rsocket. When we re-run the application, we can see
everything still works the way we expect, only this time we’re using the rsocket profile
and connecting via the RsocketStockClient. So there we have it. A full end-to-end application with a JavaFX
line chart that subscribes to a reactive stream of prices from a Kotlin Spring Boot application,
and can be configured to get those prices either via server sent events or via the new
Rsocket protocol. Thanks for watching!

Leave a Reply

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