Created
April 13, 2018 18:18
-
-
Save litvil/c36071aa4a149ca84ad12d99fea0246c to your computer and use it in GitHub Desktop.
AsyncConfiguration with custom timeout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ruby.cms.comp.aggregator.util.config; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; | |
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.core.task.AsyncTaskExecutor; | |
import org.springframework.scheduling.annotation.AsyncConfigurer; | |
import org.springframework.scheduling.annotation.EnableAsync; | |
import org.springframework.scheduling.annotation.EnableScheduling; | |
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | |
import org.springframework.web.context.request.NativeWebRequest; | |
import org.springframework.web.context.request.async.CallableProcessingInterceptor; | |
import org.springframework.web.context.request.async.TimeoutCallableProcessingInterceptor; | |
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer; | |
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; | |
import java.util.concurrent.Callable; | |
@Configuration | |
@EnableAsync | |
@EnableScheduling | |
public class AsyncConfiguration implements AsyncConfigurer { | |
private final Logger log = LoggerFactory.getLogger(AsyncConfiguration.class); | |
@Override | |
@Bean(name = "taskExecutor") | |
public AsyncTaskExecutor getAsyncExecutor() { | |
log.debug("Creating Async Task Executor"); | |
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | |
executor.setCorePoolSize(10); | |
executor.setMaxPoolSize(10); | |
executor.setQueueCapacity(100); | |
executor.setThreadNamePrefix("cms_thread_"); | |
return executor; | |
} | |
@Override | |
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { | |
return new SimpleAsyncUncaughtExceptionHandler(); | |
} | |
/** Configure async support for Spring MVC. */ | |
@Bean | |
public WebMvcConfigurerAdapter webMvcConfigurerAdapter(AsyncTaskExecutor taskExecutor, CallableProcessingInterceptor callableProcessingInterceptor) { | |
return new WebMvcConfigurerAdapter() { | |
@Override | |
public void configureAsyncSupport(AsyncSupportConfigurer configurer) { | |
configurer.setDefaultTimeout(1800000L) // 30 min | |
.setTaskExecutor(taskExecutor); | |
configurer.registerCallableInterceptors(callableProcessingInterceptor); | |
super.configureAsyncSupport(configurer); | |
} | |
}; | |
} | |
@Bean | |
public CallableProcessingInterceptor callableProcessingInterceptor() { | |
return new TimeoutCallableProcessingInterceptor() { | |
@Override | |
public <T> Object handleTimeout(NativeWebRequest request, Callable<T> task) throws Exception { | |
log.error("timeout!"); | |
return super.handleTimeout(request, task); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment