I am studying Spring webflux and faced with a situation that I am not understanding.
Spring version = 2.2.4.RELEASE My computer has 4 cores.
My Class:
@RestController
@RequestMapping("rest/v1/")
public class RootController {
private final WebClient webClient =
WebClient.create("http://localhost:8081");
@GetMapping("/booking")
public Mono book() {
return Mono.just("A");
}
@GetMapping("/sleep")
public Mono<String> sleep(@RequestParam("time") long time) throws InterruptedException {
System.out.printf("Thread name is %s, date is %s. \n", Thread.currentThread().getName(), new Date());
String uri = "/rest/v1/sleep?time=" + time;
String uuid = UUID.randomUUID().toString();
System.out.println(">>>>>>>>>>>>" + uuid);
Mono<String> stringMono = webClient.get().uri(uri)
.retrieve()
.bodyToMono(String.class);
System.out.printf("Finish -> Thread name is %s, date is %s. \n", Thread.currentThread().getName(), new Date());
return stringMono;
}
}
I have another application in my machine, running on port 8081. This endoint only sleep for a period of time that received by param.
The goal is test how is the webflux behaviour when call a slow endpoint.
I am perfoming a request like this:
http://localhost:8082/rest/v1/sleep?time=20000
In this case, the response will take 20s.
When I perform another request during this period, the controller does not process the request, it keep waiting for this request finish (20s).
Should Spring process another request while is waiting for a network call?
This is the log:
Thread name is reactor-http-epoll-3, date is Sat May 16 19:06:54 BRT 2020.
>>>>>>>>>>>>22b36a46-e36b-4104-981a-e132483a334e
Finish -> Thread name is reactor-http-epoll-3, date is Sat May 16 19:06:54 BRT 2020.
Thread name is reactor-http-epoll-3, date is Sat May 16 19:07:14 BRT 2020.
>>>>>>>>>>>>076a0eec-3bb1-485a-ab88-9b11c6b9a5bd
Finish -> Thread name is reactor-http-epoll-3, date is Sat May 16 19:07:14 BRT 2020.