简体   繁体   中英

Rest Web Service Interface - Multithreaded

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM