티스토리 뷰

Skill/spring

AsyncConfig.java

진열사랑 2025. 1. 13. 09:48

Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {
private static ThreadPoolTaskExecutor threadPoolTaskExecutor;

private static String THREAD_NAME = "ASYNC-THREAD-";

private static int CORE_POOL_SIZE = Runtime.getRuntime().availableProcessors(); // 최초에 생성할 Pool Size
private static int MAX_POOL_SIZE = Runtime.getRuntime().availableProcessors(); // 최대 Pool Size
private static int QUEUE_SIZE = 100; // 유후 스레드 수 만큼만 작업하는 방식 구현이므로 영향을 주지 않음

@Override
@Bean(name = "asyncExecutor")
public Executor getAsyncExecutor() {
threadPoolTaskExecutor = new ThreadPoolTaskExecutor();

threadPoolTaskExecutor.setCorePoolSize(CORE_POOL_SIZE);
threadPoolTaskExecutor.setMaxPoolSize(MAX_POOL_SIZE);
threadPoolTaskExecutor.setQueueCapacity(QUEUE_SIZE);
threadPoolTaskExecutor.setAllowCoreThreadTimeOut(false);
threadPoolTaskExecutor.setThreadNamePrefix(THREAD_NAME);
threadPoolTaskExecutor.initialize();

return threadPoolTaskExecutor;
}

@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new AsyncExceptionHandler();
}

public static int getIdleThreadCnt() {
int intWaitThreadCnt;

try {
intWaitThreadCnt = CORE_POOL_SIZE - (threadPoolTaskExecutor.getActiveCount());

if (intWaitThreadCnt < 0) {
intWaitThreadCnt = 0;
}
} catch(Exception ex) {
intWaitThreadCnt = 0;
}

return intWaitThreadCnt;
}
}

'Skill > spring' 카테고리의 다른 글

PropertiesCheck.java  (0) 2025.01.13
JasyptConfig.java  (0) 2025.01.13
ServiceApplication  (0) 2025.01.09
mybatis 문자열 비교  (0) 2024.12.02
java aws secretManager사용법  (0) 2024.10.22
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함