Spring Boot: How to Set Async Timeout When Deploying to an External Server

While using the embedded tomcat for deploying my spring boot app, I set the async timeout as follows:

@Bean
public EmbeddedServletContainerFactory servletContainer() {
    TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
    factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {

        @Override
        public void customize(Connector connector) {
            connector.setAsyncTimeout(60000);
        }
    });
    return factory;
}

But,how to achieve the same when deploying to an external server, for example, websphere?

Tried using the property:

spring.mvc.async.request-timeout=600000

But this did not have any effect.

Edit:

I had tried implementing AsyncConfigurer as per Andrei's suggestion. But it did not work as expected. Below is my configuration class:

@SpringBootApplication
@EnableAsync
 public class Application implements AsyncConfigurer {

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}

@Override
public Executor getAsyncExecutor() {
    Executor executor = new ThreadPoolExecutor(10, 20, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10),
            new ThreadPoolExecutor.AbortPolicy());
    return executor;
}

@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
    // TODO Auto-generated method stub
    return new SimpleAsyncUncaughtExceptionHandler();
}
 }

I have given timeout as 60 seconds, but when trying this configuration, the request was timing out after 30 seconds. Was using RestClient.

Is there something I am missing?

1 Answer

In the SpringApplication (implement first the interface called AsyncConfigurer) class I would create my custom AsyncExeuctor like this:

    @Override
    public Executor getAsyncExecutor() {
        Executor executor = new ThreadPoolExecutor(
                poolSize,
                maxSize,
                keepAlive, 
                TimeUnit.SECONDS, // <--- TIMEOUT IN SECONDS 
                new ArrayBlockingQueue<>(qSize),
                new ThreadPoolExecutor.AbortPolicy() // <-- It will abort if timeout exceeds
        );
        return executor;
    }

You can configure the poolSize, maxSize, etc. in the application.properties file and then "inject" them using the @Value annotation.

8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest