1

Below is a snippet of an existing Rest Interface implementation.

@RestController
@RequestMapping("/login")
public class LoginController {


    @Autowired
    private LoginProcessor loginProcessor;

    @RequestMapping(
        consumes = MediaType.TEXT_XML_VALUE, 
        produces = { MediaType.TEXT_XML_VALUE,
                     MediaType.APPLICATION_JSON_VALUE },
        value = "/v1/login", 
        method = RequestMethod.POST)

     public LoginResponse loginRequest(
            @RequestBody String credentials) throws JAXBException {

               return loginProcessor.request(credentials);
     }
}

If the REST call to loginRequest() is initiated from different clients, and possibly, at the same time :-

1) Will a new thread be created to handle each request. Therefore, all requests are being processed concurrently ?

or

2) Is there one thread to handle all requests, which would mean only loginRequest() is being executed at any one time, and other request are queued up ?

I would ideally like to the interface to be able to handle multiple requests at any one time.

Thank you for your help in both clarifying and furthering my understanding on the subject.

Pete

Pete Long
  • 107
  • 2
  • 11
  • Might be a duplicate of https://stackoverflow.com/questions/40794181/is-rest-controller-multithreaded – xingbin Nov 10 '18 at 18:18

3 Answers3

1

You can search stack overflow for this type of question as it has been answered before. You can read these answers: https://stackoverflow.com/a/7457252/10632970 https://stackoverflow.com/a/17236345/10632970

Good luck with your studies.

1

Every application should run in server either web server (tomcat) or application server (web logic), by default tomcat web container will have 200 threads ( you can adjust as your wish), so 200 threads can process concurrently at a time in tomcat

For every input request will be taken by web container thread and next to dispatcher servlet to corresponding controller class

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
1

I suppose you are using spring framework ( as you have used Autowired and other annotations). Thus to ans your ques: Yes spring will create new thread for each new request. Kindly refer to this answer, this should solve your queries

https://stackoverflow.com/a/17236345/7622687

NiksVij
  • 183
  • 1
  • 9
  • Hi NiksVij. Thank you for a prompt answer. My is deployed in WebLogic container so from the response, the WebLogic will create a new thread to process each request. Is there a way to confirm this is happening on WebLogic or some other way. ? – Pete Long Nov 10 '18 at 19:40
  • Hey Pete. I don't have much experience on WebLogic , but i think you should be able to confirm the above by taking help from this: https://stackoverflow.com/questions/1323408/get-a-list-of-all-threads-currently-running-in-java and adding few log statements . Hope it helps :) – NiksVij Nov 13 '18 at 16:01