0

I met strange Spring WebClient's behavior. I have two URLs, slow and fast. Bot do nothing, but slow just wait ten seconds before response. When i call them simultaneously using WebClient, i expect fast URL will be completed earlier than slow, but actually they both completed at same time. And worst of it - sometimes it works as expected. Has anybody thoughts, why it acts in that manner, and how to make it works right? Here is my example

fun main() {
    val webClient = WebClient.create()
    println("${LocalDateTime.now()} [${Thread.currentThread().name}] Start")

    webClient.get().uri("http://callback-mock/slow-url").exchange()
       .subscribe { response -> 
            println("${LocalDateTime.now()} [${Thread.currentThread().name}] Executed callback slow URL with result ${response.statusCode()}") 
       }
    webClient.get().uri("http://callback-mock/fast-url").exchange()
       .subscribe { response -> 
            println("${LocalDateTime.now()} [${Thread.currentThread().name}] Executed callback fast URL with result ${response.statusCode()}")
       }
    println("${LocalDateTime.now()} [${Thread.currentThread().name}] Waiting for exit")
    Thread.sleep(15_000)
    println("${LocalDateTime.now()} [${Thread.currentThread().name}] Exit")
}

Result (in most cases)

2019-10-02T13:04:34.536 [main] Start
2019-10-02T13:04:35.173 [main] Waiting for exit
2019-10-02T13:04:44.791 [reactor-http-nio-4] Executed callback slow URL with result 200 OK
2019-10-02T13:04:44.791 [reactor-http-nio-2] Executed callback fast URL with result 200 OK
2019-10-02T13:04:50.193 [main] Exit

Process finished with exit code 0

In rare cases it works as expected

2019-10-02T13:23:35.619 [main] Start
2019-10-02T13:23:36.300 [main] Waiting for exit
2019-10-02T13:23:36.802 [reactor-http-nio-2] Executed callback fast URL with result 200 OK
2019-10-02T13:23:45.810 [reactor-http-nio-4] Executed callback slow URL with result 200 OK
2019-10-02T13:23:51.308 [main] Exit

Process finished with exit code 0
Alexey Sviridov
  • 3,360
  • 28
  • 33

1 Answers1

1

The following very simple test shows that fast is always returned first. (Reactor Netty is used as HTTP server)

    @Test
    public void test() throws InterruptedException {
        DisposableServer server =
                HttpServer.create()
                        .port(0)
                        .route(r -> r.get("/fast", (req,res) -> res.sendString(Mono.just("test")))
                                    .get("/slow", (req,res) -> res.sendString(Mono.just("test").delayElement(Duration.ofSeconds(10)))))
                        .bindNow();

        WebClient webClient = WebClient.create();
        System.out.println(LocalDateTime.now() + " " + Thread.currentThread().getName() + " Start");

        webClient.get().uri("http://localhost:" + server.port() + "/slow").exchange()
                .subscribe(response ->
                        System.out.println(LocalDateTime.now() + " " + Thread.currentThread().getName() +
                                " Executed callback slow URL with result " + response.statusCode()));

        webClient.get().uri("http://localhost:" + server.port() + "/fast").exchange()
                .subscribe(response ->
                        System.out.println(LocalDateTime.now() + " " + Thread.currentThread().getName() +
                                " Executed callback fast URL with result " + response.statusCode()));

        System.out.println(LocalDateTime.now() + " " + Thread.currentThread().getName() + " Waiting for exit");
        Thread.sleep(15_000);
        System.out.println(LocalDateTime.now() + " " + Thread.currentThread().getName() + " Exit");

        server.disposeNow();
    }
Violeta Georgieva
  • 1,927
  • 1
  • 12
  • 13
  • Hm... possible you right, maybe something wrong with mock server... I'll check it and mark your answer if it is – Alexey Sviridov Oct 05 '19 at 07:42
  • You absolutely right, thank you a LOT. It was problem in python server, it was runned in single-thread mode. Thank you for right direction on it! – Alexey Sviridov Oct 07 '19 at 11:21