{{announcement.body}}
{{announcement.title}}

Tutorial: Reactive Spring Boot, Part 10: Spring Profiles to Switch Clients

DZone 's Guide to

Tutorial: Reactive Spring Boot, Part 10: Spring Profiles to Switch Clients

In this final installment on building a Reactive Spring Boot app, we focus on using Spring profiles to switch clients.

· Java Zone ·
Free Resource

Let's focus on using Spring profiles to switch clients.

In this lesson, we use Spring Profiles to enable an application to determine which of our two clients (server-sent events via WebClient, or RSocket) to use to connect to our Kotlin Spring Boot price service.

This is the final 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.

Here is everything you've missed so far:

Tutorial: Reactive Spring Boot, Part 1: Building a Kotlin REST Service

Tutorial: Reactive Spring Boot, Part 2: A REST Client for Reactive Streams

Tutorial: Reactive Spring Boot, Part 3: A JavaFX Spring Boot Application

Tutorial: Reactive Spring Boot, Part 4: A JavaFX Line Chart

Tutorial: Reactive Spring Boot, Part 5: Auto-Configuration for Shared Beans

Tutorial: Reactive Spring Boot, Part 6: Displaying Reactive Data

Tutorial: Reactive Spring Boot, Part 7: Subscribing Multiple Subscribers

Tutorial: Reactive Spring Boot, Part 8: Kotlin RSocket Server

Tutorial: Reactive Spring Boot, Part 9: Java RSocket Client

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.

Now we have an RSocket client that lets us connect to our RSocket server, we want to use this from our JavaFX application.

Creating the RSocketStockClient Bean

We intentionally have two implementations of ourStockClient, 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.

  1. Create a new @Bean method on ClientConfiguration in the stock-client module, called rSocketStockClient, which returns a StockClient.
  2. The body of this method needs to return a new RSocketStockClient, which will need to take an  rSocketRequester as a constructor argument.
  3. Add an RSocketRequester as a method parameter to this rSocketStockClient method.
  4. (Tip: We can get IntelliJ IDEA to add the correct method parameter if we pass an unknown variable  rSocketRequester into the RSocketStockClient constructor, press Alt+Enter on the unknown variable and select "Create parameter".)
  5. (Tip: IntelliJ IDEA Ultimate will warn you that this parameter can't be autowired because no beans match this type.)
  6. Create another @Bean method called  rSocketRequester that returns an  RSocketRequester.
  7. Declare an RSocketRequester.Builder parameter builder for the method. This should be wired in automatically by Spring.
  8. Use the builder's connectTcp method and give it "localhost" and port 7000 (that's where our Spring Boot RSocket server is running). Call block() to complete this connection.
Java




x


1
@Configuration
2
public class ClientConfiguration {
3
    // WebClientStockClient bean method...
4
 
5
    @Bean
6
    public StockClient rSocketStockClient(RSocketRequester rSocketRequester) {
7
        return new RSocketStockClient(rSocketRequester);
8
    }
9
 
10
    @Bean
11
    public RSocketRequester rSocketRequester(RSocketRequester.Builder builder) {
12
        return builder.connectTcp("localhost", 7000).block();
13
    }
14
 
15
    // WebClient bean method...
16
}


Choosing Which Bean to Use

If we go to our JavaFX ChartController (in the stock-ui module), this is the class that uses the  StockClient to connect to the price service and display prices on the chart. IntelliJ IDEA Ultimate shows 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.

  1. Add a @Profile annotation to the WebClientStockClient method, passing in a value of sse (Server-Sent Events).
  2. Give the RSocketStockClient a @Profile of "rsocket".
Java




x


 
1
@Bean
2
@Profile("sse")
3
public StockClient webClientStockClient(WebClient webClient) {
4
    return new WebClientStockClient(webClient);
5
}
6
 
7
@Bean
8
@Profile("rsocket")
9
public StockClient rSocketStockClient(RSocketRequester rSocketRequester) {
10
    return new RSocketStockClient(rSocketRequester);
11
}



Selecting the Active Profile

If we're using IntelliJ IDEA Ultimate, when we go to the ChartController, we can see the error has gone away now. But we still need to say which profile we want to use.

  1. Go to application.properties in the stock-ui module.
  2. Set the spring.profiles.active property to sse. This should give us the same bean and the same functionality that we had before.
Shell




x


 
1
# web-application and application title properties here...
2
spring.profiles.active=sse


  1. Re-run the application, the application should start up as expected and the chart should show two sets of prices as before.
  2. Note in the run window that the JavaFX application has started up with the sse profile.

Logging for Debugging

If we want to be extra sure that we're using the bean we think we're using, we could go back to the clients and add some logging.

    1. In the pricesFor method of WebClientStockClient, add an info-level log message to state that this is the WebClient stock client.
Java




x


 
1
public Flux<StockPrice> pricesFor(String symbol) {
2
    log.info("WebClient stock client");
3
    return // create Flux here...
4
}


    2. Do something similar for the RSocketStockClient.
Java




x


1
public Flux<StockPrice> pricesFor(String symbol) {
2
    log.info("RSocket stock client");
3
    return // create Flux here...
4
}


    3. Re-run the application, we should see two log messages that we're using the WebClient stock client.

Getting Prices Via RSocket

Let's finally use RSocket to get prices to display on our JavaFX line chart.

  1. Go back to the application.properties file in stock-ui and change the active profile to rsocket.
  2. Re-run the application, everything should still works the way we expect. This time we're using the rsocket profile and connecting to the RSocket price server via the RSocketStockClient.
Shell




xxxxxxxxxx
1


 
1
# web-application and application title properties here...
2
spring.profiles.active=rsocket


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.

The full code is available on GitHub:

Further Reading

Fully Reactive: Spring, Kotlin, and JavaFX Playing Together [Links]

Kotlin Microservice With Spring Boot Tutorial

Spring Reactive Programming in Java

Topics:
java ,reactive ,kotlin ,spring boot ,tutorial

Published at DZone with permission of Trisha Gee , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}