문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 | ||
springframework:async [2018/11/19 16:56] kwon37xi [설정] |
springframework:async [2023/01/26 10:20] (현재) kwon37xi [ThreadPoolTaskExecutor 설정] |
||
---|---|---|---|
줄 2: | 줄 2: | ||
* [[http:// | * [[http:// | ||
* [[https:// | * [[https:// | ||
+ | * [[java: | ||
+ | * 비동기 작업시 [[springframework: | ||
===== 설정 ===== | ===== 설정 ===== | ||
줄 20: | 줄 22: | ||
===== ThreadPoolTaskExecutor 설정 ===== | ===== ThreadPoolTaskExecutor 설정 ===== | ||
- | * [[java: | + | * [[java: |
- | * wait 설정으로 서버 종료시 남은 작업을 기다릴 시간 확보 | + | * wait 설정으로 서버 종료시 남은 작업을 기다릴 시간 확보해야함. 아래를 안해주면 서버가 그냥 종료 돼 버린다. |
* '' | * '' | ||
* '' | * '' | ||
+ | * '' | ||
+ | * [[https:// | ||
+ | * fixed? cached? | ||
+ | * 서버 종료시 올바로 종료됨을 보장하려면 [[springframework: | ||
===== Cached Thread Pool 효과 ===== | ===== Cached Thread Pool 효과 ===== | ||
- | * QueueCapacity를 0으로 만들면 Cached Thread Pool 처럼 작동한다. | + | * corePoolSize 는 적게 혹은 0, maxPoolSize='' |
* 기본 CorePoolSize 만큼 풀을 생성하고, | * 기본 CorePoolSize 만큼 풀을 생성하고, | ||
- | * 필요하면 쓰레드풀을 최대 | + | * 필요하면 쓰레드풀을 최대 |
* 안 사용하는 시간이 KeepAliveSeconds가 지나면 쓰레드를 없앤다. | * 안 사용하는 시간이 KeepAliveSeconds가 지나면 쓰레드를 없앤다. | ||
+ | * cachedThreadPool 은 느려지는 태스크 실행시 쓰레드 갯수가 폭증해서 시스템을 다운시킬 수도 있다. | ||
<code java> | <code java> | ||
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor(); | ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor(); | ||
threadPoolTaskExecutor.setThreadNamePrefix(" | threadPoolTaskExecutor.setThreadNamePrefix(" | ||
- | threadPoolTaskExecutor.setCorePoolSize(10); | + | threadPoolTaskExecutor.setCorePoolSize(0); |
threadPoolTaskExecutor.setMaxPoolSize(Integer.MAX_VALUE); | threadPoolTaskExecutor.setMaxPoolSize(Integer.MAX_VALUE); | ||
threadPoolTaskExecutor.setQueueCapacity(0); | threadPoolTaskExecutor.setQueueCapacity(0); | ||
줄 43: | 줄 49: | ||
</ | </ | ||
+ | ===== MDC 사용하기 ===== | ||
+ | * '' | ||
+ | * [[https:// | ||
+ | |||
+ | ==== ThreadPoolTaskExecutor 설정에 Decorator 지정 ==== | ||
+ | <code java> | ||
+ | @Override | ||
+ | public Executor getAsyncExecutor() { | ||
+ | ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
+ | executor.setTaskDecorator(new MdcTaskDecorator()); | ||
+ | executor.initialize(); | ||
+ | return executor; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== MdcTaskDecorator.java ==== | ||
+ | <code java> | ||
+ | class MdcTaskDecorator implements TaskDecorator { | ||
+ | |||
+ | @Override | ||
+ | public Runnable decorate(Runnable runnable) { | ||
+ | // Right now: Web thread context ! | ||
+ | // (Grab the current thread MDC data) | ||
+ | Map< | ||
+ | return () -> { | ||
+ | try { | ||
+ | // Right now: @Async thread context ! | ||
+ | // (Restore the Web thread context' | ||
+ | if (contextMap != null) { | ||
+ | MDC.setContextMap(contextMap); | ||
+ | } | ||
+ | |||
+ | runnable.run(); | ||
+ | } finally { | ||
+ | MDC.clear(); | ||
+ | } | ||
+ | }; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== CompletableFuture 지원 ===== | ||
+ | * [[java: | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | |||
+ | * 아래와 같이 '' | ||
+ | <code java> | ||
+ | @Async | ||
+ | public CompletableFuture< | ||
+ | logger.info(" | ||
+ | String url = String.format(" | ||
+ | User results = restTemplate.getForObject(url, | ||
+ | Thread.sleep(1000L); | ||
+ | return CompletableFuture.completedFuture(results); | ||
+ | } | ||
+ | </ |