Spring-Boot: accessing multiple requests simultaneously

I am using Spring Download to create a RESTful web service . My IDE is Eclipse Oxygen.

I send several HTTP requests every 2 seconds via Chrome , but they are triggered one by one . Each request will wait for the completion of the previous request.

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 console:

 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 indicates that the controller is called every 5 seconds . But I send requests every 2 seconds .

How can I handle multiple incoming requests at the same time? (I should see console output every 2 seconds)

UPDATE

If I send requests in different browsers, it works fine. If I test it in the same browser / application that shares the session, a problem will occur.

Is it possible to accept simultaneous multiple requests from the same session ?

Thanks!

+5
source share
2 answers

By default, Spring. Downloadable web applications are multithreaded and process multiple requests at the same time.

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

+4
source

Spring beans are singleton beans by default. Singleton means one instance of an object per container. So in one container there is one instance of DummyController. The first request arrives, and the thread goes to sleep for 5 seconds. As soon as it wakes up, full execution is performed and the object will be picked up by the second request. Check Is the REST controller multithreaded?

-3
source

Source: https://habr.com/ru/post/1271804/


All Articles