개발

[spring] @Async 사용하기

으아아아앜 2020. 2. 19. 03:52

특징

  • @Async를 적용하는 메소드는 반드시 public 메소드여야함
  • 같은 클래스의 메소드가 호출시 동작하지 않음(프록시 관련 문제)
  • @Async를 적용하는 메소드의 return 타입 => Future<> 또는 void

사용법

@Configuration
@EnableAsync
public class AsyncConfig {
    @Bean(name = "threadPoolTaskExecutor")
    public Executor threadPoolTaskExecutor() {
        return new ThreadPoolTaskExecutor();
    }
}
@Async("threadPoolTaskExecutor")
public void asyncMethodWithConfiguredExecutor() {
    System.out.println("Execute method with configured executor - "
      + Thread.currentThread().getName());
}

 

 

참고 :

https://www.baeldung.com/spring-async  

https://jeong-pro.tistory.com/187