Advanced Asynchronous Execution and Performance Tuning in Spring Boot
This article explains multiple Spring Boot asynchronous execution techniques—such as @Async, CompletableFuture, WebAsyncTask, DeferredResult, and AsyncHandlerInterceptor—provides complete Java code examples, and discusses performance optimizations like increasing Tomcat connection limits, switching to Undertow, using ComponentScan, and buffered I/O.
The article, written by a senior architect, introduces several ways to perform asynchronous execution in Spring Boot, including the @Async annotation with @EnableAsync, CompletableFuture, supplyAsync, runAsync, WebAsyncTask, DeferredResult, and DeferredResult with timeout handling.
It provides complete Java code examples for each method, demonstrating thread creation, future handling, and controller implementations.
Key code snippets:
@Async
@EnableAsync
public class AsyncConfig { ... } CompletableFuture<Integer> future = new CompletableFuture<>();
new Thread(new AskThread(future)).start();
future.complete(60);
System.out.println(future.get()); @RestController
public class HelloController {
@GetMapping("/helloworld")
public String helloWorldController() {
return hello.sayHello();
}
}Additional performance optimizations are discussed, such as increasing Tomcat's maximum connections and threads via a custom TomcatConnectorCustomizer, switching the embedded server from Tomcat to Undertow, using @ComponentScan for faster package scanning, and employing BufferedWriter for I/O buffering.
@Configuration
public class TomcatConfig {
@Bean
public ConfigurableServletWebServerFactory webServerFactory() {
TomcatServletWebServerFactory tomcatFactory = new TomcatServletWebServerFactory();
tomcatFactory.addConnectorCustomizers(new MyTomcatConnectorCustomizer());
return tomcatFactory;
}
}
class MyTomcatConnectorCustomizer implements TomcatConnectorCustomizer {
public void customize(Connector connector) {
Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
protocol.setMaxConnections(20000);
protocol.setMaxThreads(2000);
}
}The article also shows how to intercept asynchronous requests with AsyncHandlerInterceptor and outlines configuration classes for customizing the servlet container.
References and further reading links are included at the end of the article.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
