Spring Boot:如何在部署到外部服务器时设置异步超时

时间:2016-10-13 09:18:15

标签: java spring asynchronous spring-boot

使用嵌入式tomcat部署我的spring启动应用程序时,我按如下方式设置异步超时:

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

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

但是,如何在部署到外部服务器时实现相同的目标,例如,websphere?

尝试使用该属性:

spring.mvc.async.request超时= 600000

但这没有任何效果。

修改

根据Andrei的建议,我曾尝试过实施AsyncConfigurer。但它没有按预期工作。以下是我的配置类:

@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();
}
 }

我已将超时时间设为60秒,但在尝试此配置时,请求在30秒后超时。使用RestClient。

我有什么遗失的吗?

1 个答案:

答案 0 :(得分:0)

在SpringApplication(首先实现名为AsyncConfigurer的接口)类我将创建我的自定义AsyncExeuctor:

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

您可以在application.properties文件中配置poolSizemaxSize等,然后使用@Value注释“注入”它们。