반응형
개발중인 서비스중 외부업체 API를 호출할일이 있어 테스트 하던중 중복 호출이 100번 이상 호출할일이 생겨서
위의 경우 동기식일 경우 API 한번호출시 1초가 걸린다고 가정하였을경우 최소 100초 지나야 호출한곳으로 결과를 반환할수가 있다
그래서 비동기로 호출하는 테스트 코드를 작성해 보았다.
public void multiMonoToFlux() {
//WebClient Wrapper class
XmlWebClient xmlWebClient = new XmlWebClient();
List<Mono<XmlResponse>> monos = new ArrayList();
IntStream.range(1,10).forEach(idx -> {
log.debug(String.format("WebFlux Call Start[%d]",idx));
monos.add(xmlWebClient.findServiceByParameter(XmlResponse.class, idx));
log.debug(String.format("WebFlux Call End[%d]",idx));
});
Flux<XmlResponse> responseFlux = Flux.merge(monos);
responseFlux.subscribe(response -> System.out.println("responseString = " + response));
}
위 findServiceByParameter는 외부 API를 호출하고 Mono를 반환한다.
10번 호출한 Mono값을 Flux.merge로 Flux를 얻어 subscribe을 한다.
즉 외부 API를 비동기로 호출한후 그중 먼저 응답온 결과를 subscibe으로 처리한다.
위 예제 코드 github
https://github.com/hiphopddori/spring-webflux
hiphopddori/spring-webflux
webflux study & test code. Contribute to hiphopddori/spring-webflux development by creating an account on GitHub.
github.com
반응형
'BackEnd > spring' 카테고리의 다른 글
Springboot dockerfile 없이 이미지 생성하기 bootBuildImage (0) | 2021.06.28 |
---|---|
Webclient retry Test 방법 (0) | 2021.06.14 |
xss Filter emoji parse error (1) | 2021.03.02 |
git 디렉토리 삭제 하고 싶을때 (0) | 2021.02.18 |
gradle api 사용시 오류 발생시 확인 사항 (0) | 2021.02.16 |