简体   繁体   中英

Does REST APIs defined in Spring Boot handles multi threading automatically

I want to create a REST API in spring boot which can handle around 100TPS. Lets say I create a basic REST api using a sample application in spring boot. Does this automatically handles multi threading. Lets consider the code bellow.

@RequestMapping(method = RequestMethod.POST, value="findByPackageActivationId")
@ResponseBody
public JSONObject findByPackageActivationId(@RequestBody IncomingRestObject incomingRestObject) {
    //My work here
}

By default Spring Boot web applications are multi-threaded and will handle multiple requests concurrently.REST controller is multithreaded as the DisptcherServlet handles multiple requests from the clients concurrently and serves using the respective controller methods.You can change any of the default thread settings as well (eg server.tomcat.max-threads). For more information refer here :-

https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc

For every request from the client, the server will create a new thread and assign that request to newly created thread. You don't have to handle this explicitly.

Also for your requirement(100 Threads), you can configure this on the server. You can refer this link for tomcat configuration as its default server for spri.

REST controller is multithreaded as the DisptcherServlet handles multiple requests from the clients concurrently and serves using the respective controller method

You can scale your app as per your requirements to achive desired TPS

Spring Framework provide these feature internally, these are benefits that spring provide over core servlet coding that we used to do in older days. It also provide the way to control it. For Ex you can change limit of number of thread to create using server.tomcat.max-threads (For tomcat).

For Basic understading on multi threading with spring boot please refer https://www.e4developer.com/2018/03/30/introduction-to-concurrency-in-spring-boot/

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