본문 바로가기

TIL/트러블슈팅

[SpringBoot]비동기 Async는 프록시 기반 동작

 

👉 에러

비동기(@Async) 실행을 위한 TaskExecutor를 찾지 못하는 문제

 NFO 13248 --- .s.a.AnnotationAsyncExecutionInterceptor :
More than one TaskExecutor bean found within the context, and none is named 'taskExecutor'.  
Mark one of them as primary or name it 'taskExecutor' (possibly as an alias) in order to use it for async processing:
[clientInboundChannelExecutor, clientOutboundChannelExecutor, brokerChannelExecutor]

 

👉 원인

 

  • spring이 WebSocket(STOMP) 관련 TaskExecutor(clientInboundChannelExecutor, clientOutboundChannelExecutor, brokerChannelExecutor)를 자동으로 생성하고 있음.
  • 하지만 @Async에서 사용할 기본 TaskExecutor가 taskExecutor로 명시되지 않아서 어떤 것을 사용할지 결정하지 못함.

 

 

🚀 해결 방법

taskExecutor를 명시적으로 지정

1️⃣ AsyncConfig 클래스를 만들어 TaskExecutor를 명확하게 지정

@Bean(name = "mailExecutor")
public Executor mailExecutor() 

 

2️⃣ 비동기 실행 시 명시적으로 taskExecutor 사용

@Async("mailExecutor")
public void sendMailWithPdf(String  novelId, User user,List<String> fileNames, List<byte[]> fileDataList) throws MessagingException {