본문 바로가기
IT/Spring

No qualifying bean of type 'org.springframework.core.task.TaskExecutor' available: expected single matching bean but found 2: applicationTaskExecutor, taskScheduler

by 최고영회 2022. 3. 2.
728x90
반응형
SMALL

Spring boot 에서 @Async 와 @Scheduled 를 동시에 사용할 때 

 

No qualifying bean of type 'org.springframework.core.task.TaskExecutor' available: expected single matching bean but found 2: applicationTaskExecutor, taskScheduler

 

이런 로그를 볼 수 있다. 

@Async 기본설정은 SimpleAsyncTaskExecutor 를 사용하도록 되어 있고 

@Scheduled 는 spring 에 의해 생성된 한개의 thread-pool 에서 실행된다.

Spring 에서 생성한 thread-pool 과 SimpleAsyncTaskExecutor 모두 'org.springframework.core.task.TaskExecutor' type 이기에 bean 을 찾을 때 어떤걸 찾아야 하는지 몰라서 발생하는 오류 로그이다. 

 

AsyncAnnotationBeanPostProcessor 참고 

try {
  // Search for TaskExecutor bean... not plain Executor since that would
  // match with ScheduledExecutorService as well, which is unusable for
  // our purposes here. TaskExecutor is more clearly designed for it.
  executorToUse = beanFactory.getBean(TaskExecutor.class);
} catch (NoUniqueBeanDefinitionExceptionex) {
...
}

 

@Async 사용 시 정확히 네이밍 해 주면 문제가 해결된다. 

@Async(value = "applicationTaskExecutor")

다만, @Async 기본설정인 SimpleAsyncTaskExecutor 는 thread-pool 을 사용하지 않기 때문에 

pool 을 사용하면서 해결하기 위해서 AsynConfigurer 를 implements 하는 configuration 을 구현하는 것이 더 좋다. 

@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {

	@Override
	public Executor getAsyncExecutor() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.setThreadNamePrefix("async-thread-");
		executor.setCorePoolSize(8);
		executor.setMaxPoolSize(10);
		executor.setQueueCapacity(100);
                executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //추후 설명
		executor.initialize();
		return executor;
	}
}

 

728x90
반응형
LIST