I’ve got a problem of understandin within the following scenario, therefore seeking for clarification.
I’ve got a SpringBoot-Application using Web MVC.
Dependencies come like this:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
I know that adding both spring-boot-starter-web and spring-boot-starter-webflux modules in auto-configures Spring MVC over Spring WebFlux, explained here.
The app is providing a Rest-Controller, such as:
@RestController
public class EmployeeRestController {
@Autowired
private EmployeeService service;
@GetMapping("/api/employee/get/{id}")
public Employee getEmployee(@PathVariable("id") int id) {
return service.fetchEmployeeData(id);
}
}
The getEmployee
method uses the EmployeeService
, which then should communicate with a 3rd party API through Spring’s Reactive WebClient
(because RestTemplate
is deprecated). The service looks like this:
@Service
public class EmployeeService {
private final WebClient webClient;
public EmployeeService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("https://mydatasource.com/employees").build();
}
public Employee someRestCall(int id) {
Employee result = this.webClient
.get()
.url(id)
.retrieve()
.bodyToMono(Employee.class)
.block();
// do other service-related stuff here
return result;
}
}
You might have already noticed the block
-Operation, which should usually not be used in Reactive programming to not block threads/application.
I came to the assumption, that it wouldn’t matter using non-blocking mechanisms as Spring Web MVC runs on a blocking stack, following a one thread per request pattern. Because of this, the block-Operation would only block the assigned thread of the ongoing request – correct? (This is backed with a great visualization in section 1 of this blog.)
Therefore my question: Is my understanding/assumption correct? If so, how could I easily prove it?