Skip to content

Instantly share code, notes, and snippets.

@stella6767
Created August 18, 2022 05:11
Show Gist options
  • Save stella6767/5f28373d93bf4e178fb9527b2604e32c to your computer and use it in GitHub Desktop.
Save stella6767/5f28373d93bf4e178fb9527b2604e32c to your computer and use it in GitHub Desktop.
스프링부트 비동기 설정
@EnableScheduling
@EnableAsync
@Configuration
class AsyncConfig(
) : AsyncConfigurerSupport() {
/**
* 최초 coreSize개의 스레드에서 처리하다가 처리 속도가 밀릴 경우
* queueCapacity 사이즈 queue에서 대기하고 그보다 많은 요청이 들어올 경우
* 최대 maxSize 스레드까지 생성해서 처리하게 된다.
* https://jeonyoungho.github.io/posts/ThreadPoolTaskExecutor/
*/
val coreSize = 10
val maxSize = 50
val queueCapacity = 10
val threadPrefix = "mulief-"
val scheduledPrefix = "mulief-scheduled"
override fun getAsyncExecutor(): Executor {
val executor = ThreadPoolTaskExecutor()
executor.corePoolSize = coreSize
executor.maxPoolSize = maxSize
executor.queueCapacity = queueCapacity
executor.setThreadNamePrefix(threadPrefix)
executor.setRejectedExecutionHandler(ThreadPoolExecutor.CallerRunsPolicy())
executor.initialize()
return executor
}
override fun getAsyncUncaughtExceptionHandler(): AsyncUncaughtExceptionHandler {
return CustomAsyncExceptionHandler()
}
@Bean
fun schedulerTasks(): ThreadPoolTaskScheduler {
val threadPoolTaskScheduler = ThreadPoolTaskScheduler()
threadPoolTaskScheduler.poolSize = coreSize
threadPoolTaskScheduler.setThreadNamePrefix(scheduledPrefix)
threadPoolTaskScheduler.initialize()
return threadPoolTaskScheduler
}
class CustomAsyncExceptionHandler : AsyncUncaughtExceptionHandler {
val log = KotlinLogging.logger { }
override fun handleUncaughtException(ex: Throwable, method: Method, vararg params: Any?) {
log.error { """
Exception message: ${ex.message}
Method Name: ${method.name}
""".trimIndent() }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment