Reputation: 123
I am currently developing a spring boot application which must handle as much as possible http requests at the same time. This is why I would like to modify the number of threads.
In my case I need to do it programmatically. I do not really now how to handle it because I am new to Tomcat and Springboot.
Here I provide my main application code which must run in port 80.
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@Component
public class CustomContainer implements EmbeddedServletContainerCustomizer {
public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {
configurableEmbeddedServletContainer.setPort(80);
}
}
}
Thanks in advance!
Upvotes: 0
Views: 2900
Reputation: 143
Which embedded server are you using? You can use the property in Tomcat server.tomcat.max-threads
to control the number of threads. 0
defaults to 200 threads used by Tomcat.
When you are saying
I am currently developing a spring boot application which must handle as much as possible http requests at the same time.
Do you care cost of performance with way too many request being sent to a shared resource e.g. a database?
If you don't want to tweak the raw number but to have as many HTTP threads to go through and be processed, you should look into making asynchronous call in Spring.
To Enable Asynchronous Calls in a SpringBootApplication do the following:
@EnableSync
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
Now you'll be able to use the method which return Completable<Future>
in your service class.
Apart from this, you really should not really handle the number of threads from your web service layer. It should really be handled by the application server (Tomcat, Glassfish etc) It will handle and make request to your methods concurrently.
If a bottleneck exist in your code, on the same hardware, increasing the number of threads will not increase the performance, instead with possibility of diminishing it.
Look into here: Spring MVC Rest Services - Number of Threads (Controller Instances)
Reference: https://www.e4developer.com/2018/03/30/introduction-to-concurrency-in-spring-boot/ https://spring.io/guides/gs/async-method/
Upvotes: 1