Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
816 views
in Technique[技术] by (71.8m points)

java - Spring-Boot: Handle multiple requests concurrently

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...