본문 바로가기

토비의봄13

10. Mono & Flux 1. Mono Mono로 감싸놓은것이 어떤의미인가? 아래의 코드는 중, mono 부분은 Subscriber가 subscribe 하는 순간에 실행됨 @GetMapping("/") Mono hello() { log.info("pos1"); Mono m = Mono.just("Hello WebFlux").log(); log.info("pos2"); return m; } 더보기 결과 출력 pos1 pos2 onSubscribe() request(unbounded) => backPressure onNext(Hello WebFlux) onComplete() Mono.just() 는 준비된 결과를 필요로 하므로 아래의 순서로 수행 @GetMapping("/") Mono hello() { log.info("pos1").. 2021. 3. 15.
9. Webflux 1. WebClient 비동기 nonblocking 방식으로 호출이 가능하다. Mono / Flux 를 리턴한다. Mono 는 Publisher 의 구현체이므로, Subscriber가 subscribe를 해야 실행된다. 해당 작업은 함수의 리턴타입이 Mono 인 경우, WebFlux가 subscribe를 호출해주므로, 우리는 Mono 타입으로 리턴하면 된다. 타입을 변경할 경우에는, map() / flatMap() 을 사용해서 변경해보자 @RestController public static class MyController { @Autowired MyService myService; WebClient client = WebClient.create(); @GetMapping("/rest") public M.. 2021. 3. 11.
8. CompletableFuture 1. CompletableFuture Future (java) 비동기적인 결과를 담고있는 오브젝트 ListenableFuture (spring) Future + Callbacks CompletableFuture 간단하게 비동기적인 결과를 가져올 수 있음 Block 방식 CompletableFuture.get() 을 이용해서 결과값을 가져오는 방식 public static void main(String[] args) throws ExecutionException, InterruptedException { CompletableFuture f = new CompletableFuture(); f.complete(2); f.completeExceptionally(new RuntimeException()); Sys.. 2021. 3. 9.
7. AsyncTemplate 의 콜백헬, 중복작업 문제 1. 콜백헬 예시와 해결 코드의 근본적인 문제 ListenableFuture 를 리턴하고 콜백을 추가하기 때문에 아래와 같은 콜백헬 문제가 생김 에러를 처리하는 부분이 계속 중복이 된다. @GetMapping("/rest") public DeferredResult rest(int idx) { DeferredResult dr = new DeferredResult(); ListenableFuture f1 = rt.getForEntity("http://localhost:8081/service?req={req}", String.class, "hello " + idx); f1.addCallback(s -> { ListenableFuture f2 = rt.getForEntity("http://localhost:80.. 2021. 3. 8.