This lab refactors the MVC application to use a microservice.
What you will learn:
Specific subjects you will gain experience with:
Estimated time to complete: 25 minutes
This lab runs a cut-down version of the REST server you wrote in an earlier lab.
Everything else involves running as JUnit tests.
The logging output of the Unit tests is important. A typical log
message is shown below. The item in square brackets is the name of
the thread producing the output, in this case reactor-http-nio-4
. The thread the
unit-tests run in is [main]
.
[reactor-http-nio-4] INFO :
rewards.internal.account.Account - Creating new Account
…
SimpleReactiveTests.java
and
run it (TODO-01). It uses both JDK streams and a Reactive
Flux.Account
object by
Spring. The Account
constructor logs
the account creation. Note that it is also using [main]
thread.Mono<Account>
. Use the example in the
slides to help you.Account
constructor logs in a different thread. You have written
a multi-threaded application without using Thread or Callable or
…bodyToFlux(Account.class)
method.subscribe()
to count each item using counter.incrementAndGet()
.RestTemplate
and then
using a WebClient
.RestTemplate
code. Very straightforward. Since there are only 21 accounts we use
id % TOTAL_ACCOUNTS
to generate the
account id to fetch, as id iterates from 0 - 199.WebClient
code is
more involved. We use the doOnSuccess()
method which is just a
callback that gets invoked for each item returned in the Flux. Here
we increment a counter each time it is invoked. When we reach 200
we determine how long the whole test took to run.RestTemplate
is quickest due to the overhead
of multi-threading. The WebClient
is
not providing any advantage - yet.AccountsController
find TODO-10
and uncomment the line to add a 50s
delay ot each response.Note: Since we only have a Spring MVC controller we are stuck with the delay we introduced, locking up threads from our thread pool. However if we converted it to a WebFlux reactive controller, we could mitigate the delay by returning a Mono (putting the delay in the Mono’s callback code). The delay is still there (as it would be if you had to perform a slow database lookup or talk to another system) but the Controller method can return immediately and free-up its thread.
Congratulations, you have finished the lab.