0

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. 
javaTry
  • 1,085
  • 2
  • 18
  • 30
  • Yes, it should process another request – mslowiak May 16 '20 at 23:37
  • 2
    Did you hit your endpoint from Chrome browser? I experienced similar issue, not sure about the reason, though. As I recall it worked well either in Firefox or Edge, though. You can also call it programmatically from another app. Edit: https://stackoverflow.com/a/53236741/6051176 – Martin Tarjányi May 17 '20 at 07:24
  • 1
    Also, it might be interesting to see how the sleep is implemented in your 'slow' service. Is it possible that is blocking other concurrent requests? – Martin Tarjányi May 17 '20 at 07:25
  • 1
    https://stackoverflow.com/a/27514611/6051176 – Martin Tarjányi May 17 '20 at 07:33

1 Answers1

1

The problem was a chrome issue that has queued my request like "Martin Tarjányi" said in his comment:

Did you hit your endpoint from Chrome browser? I experienced similar issue, not sure about the reason, though. As I recall it worked well either in Firefox or Edge, though. You can also call it programmatically from another app. Edit: stackoverflow.com/a/53236741/6051176

Thanks a lot @Martin Tarjányi

Request using other clients or change parameter works well. The same resource chrome will queue request.

javaTry
  • 1,085
  • 2
  • 18
  • 30