16

I am using Spring Boot to build a RESTful web service. My IDE is Eclipse Oxygen.

I send multiple HTTP get requests in every 2 seconds through Chrome, but they are triggered one by one. Each request will wait for the previous request to finish.

Here is my controller code:

@RestController
@RequestMapping("/dummy")
public class DummyController {
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<Map<String, String>> dummytsp(@RequestParam(value="msg", defaultValue="Hello") String msg) {
        System.out.println("" + new Date() + ": ThreadId " + Thread.currentThread().getId());

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Map<String, String> response = new HashMap<>();
        response.put("message", msg);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }
}

My console out put is:

Thu Sep 14 11:31:15 EDT 2017: ThreadId 25
Thu Sep 14 11:31:20 EDT 2017: ThreadId 26
Thu Sep 14 11:31:25 EDT 2017: ThreadId 28
Thu Sep 14 11:31:30 EDT 2017: ThreadId 30

The console output shows that the controller is called every 5 seconds. But I'm sending the requests every 2 seconds.

How could I handle multiple incoming requests concurrently? (I should see the console output every 2 seconds)

UPDATE:

If I send requests in different browsers, it works perfectly. If I test it in the same browser/application which shares the session, the problem will come out.

Is it possible to accept concurrent multiple requests from same session?

Thanks!

Top.Deck
  • 1,077
  • 3
  • 16
  • 31
  • I can't reproduce this. By default Spring Boot web applications are multi-threaded and will handle multiple requests concurrently. Are you using Embedded Tomcat? Have you changed any of the default thread settings (e.g. `server.tomcat.max-threads`)? – Kyle Anderson Sep 14 '17 at 16:40
  • @KyleAnderson the code works fine if I send the requests from different browsers. The problem shows up when I send the requests (open tabs) in the same browser. I updated my post pls check. ty! – Top.Deck Sep 14 '17 at 16:47
  • 1
    This _might_ be a browser specific quirk. On Windows 10, Chrome & Firefox do seem to queue multiple requests to the same URL, while IE, Edge, & curl do not. – Kyle Anderson Sep 14 '17 at 17:01
  • 3
    @KyleAnderson yes you are right. I am using chrome which gives me the problem, but curl & works fine. Do you mind to put your comment as answer so that I could accept your answer. – Top.Deck Sep 14 '17 at 17:06

1 Answers1

19

By default Spring Boot web applications are multi-threaded and will handle multiple requests concurrently.

This might be a browser specific quirk. On Windows 10, Chrome & Firefox do seem to queue multiple requests to the same URL, while IE, Edge, & curl do not.

Kyle Anderson
  • 6,801
  • 1
  • 29
  • 41
  • Hi @Kyle Anderson So i don't need to used Executor or Thread explicitely – Ninad Kulkarni Dec 01 '19 at 04:27
  • How to handle 1000+ request at the same time? Supposing that the service only returns "hello world" and doesn't do any other processing? I tried changing supposedly default max threads from 200 to 800+ but TPS will not increase, thank you. – Diego Ramos Mar 20 '22 at 02:03