0
votes

I am new to Spring-Webflux.

I am running the below sample project in a 2 - core processor. My RESTful api will call to an external api and the external api response is less than 500ms.

When i using jMeter for load testing, can't achieve more than 15 TPS.

Am i missing something or anything wrong with the below design?

How effective it's to override the default Threadpool executor in Webflux project?

Thank you in advance.

Controller.java
----------------

LookupController
{

@Autowired
private LookupService lookupService;
@PostMapping(value = "/findOrder")
public Mono> findOrder(@RequestBody InputBean inputBean) {

return lookupService.findOrder(inputBean)
.map(resp -> ResponseEntity.ok(resp))
.defaultIfEmpty(ResponseEntity.notFound().build());
}
}

Service
---------

@Service
public class LookupService
{
private RestWorker restWorker;
public Mono findOrder(InputBean inputBean)
{
..//Basic Validation
ApiBean apiBean = restWorker.buildApiBean(inputBean);
Mono responseStr = restWorker.callApi(apiBean);
return responseStr.flatMap(resp -> {
//Business Logic
//Process the api response and create the corresponding Controller Response
return Mono.just(controllerResponse);
});
}

}

Helper
---------

@Component
public RestWorker {

private Webclient webClient = null;

@PostConstruct
private void initWorker() {
webClient = WebClient.builder()
.baseUrl(httpUrl)
.defaultHeader(HttpHeaders.CONTENT_TYPE, "application/json")
.defaultHeader(HttpHeaders.ACCEPT, "application/json")
.defaultHeader(HttpHeaders.ACCEPT_CHARSET, "UTF-8")
.build();
}

public ApiBean buildApiBean(InputBean inputBean) {
// Create ApiBean based on the input bean + config values
....
return apiBean;
}

public Mono callApi(ApiBean apiBean) {
Mono responseMonoStr = null;
try {
responseMonoStr = webClient.post()
.uri(url_tibco)

.body(BodyInserters.fromObject(reqDoc))

.exchange()

.timeout(Duration.ofMillis(socketReadTimeout))
.flatMap(clientResponse -> {

System.out.println(Thread.currentThread().getName() + "Status Code : " + clientResponse.statusCode());
return clientResponse.bodyToMono(String.class);

});
}catch(Exception exception){
return Mono.just("");
}

}

}

2

2 Answers

1
votes

It is not, as Spring WebFlux is not using a Threadpool executor to dispatch web requests. It is rather using other server resources, like event loops (for Netty). For more information on that, you can check the Spring Boot reference documentation on reactive server resources and more importantly learn about the concurrency model in Spring WebFlux.

0
votes

Brian Clozel,

Thank you so much. Non-blocking api is working based on event loop with small number of threads. But, when am sending some parallel requests it opens 2 different threads like reactor-http-nio-2, reactor-http-nio-3..

Why it opening different threads on each parallel request?