Java – spring boot: handle multiple requests at the same time
I use spring boot to build a restful web service My IDE is eclipse oxygen
I send HTTP get requests every 2 seconds through chrome, but they trigger one by one Each request will wait for the previous request to complete
This 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 output 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 display controller is called every 5 seconds But I send a request every two seconds
How can I handle multiple incoming requests at the same time? (I should watch the console output every 2 seconds)
to update:
If I send requests in different browsers, it works perfectly If I test it in the same browser / application of the shared session, the problem will arise
Can concurrent multiple requests from the same session be accepted?
thank you!
Solution
By default, spring boot web applications are multithreaded and can process multiple requests at the same time
This may be a browser - specific quirk On Windows 10, Chrome & Firefox seems to queue multiple requests to the same URL, while ie, edge and & curl do not