Posts Async
Post
Cancel

Async

Docker

Java -> @Async 비동기 서비스

1
2
// @EnableAsync로 비동기 기능 활성화
// 비동기를 원하는 메소드는 public이어야 한다.

@Configuration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Configuration
@EnableAsync
public class AsyncConfig extends AsyncConfigurerSupport {

    
    public Executor getAsyncExecutor() {
        // ThreadPoolTaskExecutor로 비동기로 호출하는 Thread 대한 설정을 한다
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // corePoolSize: 기본적으로 실행을 대기하고 있는 Thread의 갯수
        executor.setCorePoolSize(2);
        // MaxPoolSise: 동시 동작하는, 최대 Thread 갯수
        executor.setMaxPoolSize(10);
        //QueueCapacity : MaxPoolSize를 초과하는 요청이 Thread 생성 요청시 해당 내용을 Queue에 저장하게 되고, 사용할수 있는 Thread 여유 자리가 발생하면 하나씩 꺼내져서 동작하게 된다.
        executor.setQueueCapacity(500);
        //ThreadNamePrefix: spring이 생성하는 쓰레드의 접두사를 지정한다.
        executor.setThreadNamePrefix("hanumoka-async-");
        executor.initialize();
        return executor;
    }
}

@Async를 적용한 Service

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
@Service
public class AsyncService {

    private static final Logger logger = LoggerFactory.getLogger(AsyncService.class);

    //비동기로 동작하는 메소드
    
    public void onAsync() {
        try {
            Thread.sleep(5000);
            logger.info("onAsync");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    //동기로 동작하는 메소드
    public void onSync() {
        try {
            Thread.sleep(5000);
            logger.info("onSync");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

@Async Return Type

  1. void
  2. CompletableFuture
  3. ListenableFuture
  4. Future

@Async 참조 링크

  1. Return Type
  2. Configuration 설정
  3. Simple & Configuration
저자는 이 저작물에 대해 CC BY-NC 4.0 라이선스를 가집니다.