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.

Top Architect
Top Architect
Top Architect
Advanced Asynchronous Execution and Performance Tuning in Spring Boot

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaAsynchronousSpring BootTomcat
Top Architect
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.