Search
Duplicate

비동기 닡은?

System.out.println(webClient.get() //1 .uri("/mail/test") .retrieve() .bodyToMono(String.class) .subscribe(s -> System.out.println("s = " + s))); //2 System.out.println("\"HI!!\" = " + "HI!!"); //3
Java
볡사
좜λ ₯ μˆœμ„œλŠ”?
1 β†’ 3 β†’ 2!

Mono vs Flux

β€’
Mono λŠ” 0~1 개의 데이터λ₯Ό μ „λ‹¬ν•˜κ³  Flux λŠ” 0~N 개의 데이터λ₯Ό μ „λ‹¬ν•œλ‹€.
@Test public void givenFluxPublisher_whenSubscribeThenReturnMultipleValuesWithError() { Flux<String> stringFlux = Flux.just("Hello", "Baeldung", "Error") .map(str -> { if (str.equals("Error")) throw new RuntimeException("Throwing Error"); return str; }); StepVerifier.create(stringFlux) .expectNext("Hello") .expectNext("Baeldung") .expectError() .verify(); }
Java
볡사
β€’
이에 λŒ€ν•΄μ„œλŠ” 좔후에 더 κ³΅λΆ€ν•΄λ³΄μž

block vs subscribe

β€’
Mono λ‚˜ Flux 둜 λ°”κΎΌ λ‹€μŒ μš”μ²­μ„ 보낼 λ•Œ μ‚¬μš©ν•œλ‹€.
β€’
block 은 λΈ”λ‘œν‚Ή, subscribe λŠ” λ…ΌλΈ”λ‘œν‚Ήμ΄λ‹€.
β€’
subscribe λŠ” λ…ΌλΈ”λ‘œν‚ΉμœΌλ‘œ μ²˜λ¦¬λ˜μ–΄ 응닡이 였면 응닡에 λŒ€ν•΄ μΆ”ν›„ μ²˜λ¦¬κ°€ κ°€λŠ₯ν•˜λ‹€.

block 이 κ°€λŠ₯ν•˜λ‹€λ©΄ feign 과의 μ°¨μ΄λŠ” λ¬΄μ—‡μΌκΉŒ?

The key difference between the Feign client's blocking approach and the WebClient's block() method is in their underlying mechanisms and programming models:
1.
Programming Model:
β€’
Feign: Feign is a declarative HTTP client that allows you to define interfaces with annotated methods representing remote service endpoints. It uses a blocking I/O model, where each request is executed synchronously, blocking the calling thread until the response is received or an error occurs.
β€’
WebClient: WebClient is a non-blocking, reactive HTTP client that provides a functional and fluent API for making HTTP requests. By default, it operates in a non-blocking manner using reactive programming, but you can use the block() method to temporarily switch to a blocking mode.
2.
Thread Model:
β€’
Feign: Feign uses a blocking I/O model, where the calling thread is blocked until the response is received. This means that the thread is occupied and cannot perform other tasks during the blocking period.
β€’
WebClient with block(): The block() method in WebClient allows you to temporarily switch to a blocking mode and block the calling thread until the response is received. However, the underlying WebClient itself still operates in a non-blocking manner and does not block threads outside of the block() call.
3.
Reactiveness:
β€’
Feign: Feign does not natively support reactive programming. It operates synchronously and is not designed for reactive applications.
β€’
WebClient with block(): WebClient, when used with the block() method, can be used in reactive applications with a mix of reactive and blocking code. However, it's important to note that using block() introduces blocking behavior, which can impact the responsiveness and scalability of reactive systems. It should be used judiciously and with caution.
In summary, Feign is a blocking HTTP client with a synchronous programming model, while WebClient is a non-blocking, reactive HTTP client by default. The block() method in WebClient allows you to temporarily switch to a blocking mode, but it does not fundamentally change the non-blocking nature of WebClient itself.
When working in a reactive environment, it's generally recommended to embrace the non-blocking nature of WebClient and utilize reactive programming constructs instead of relying heavily on the block() method. However, there may be scenarios where using block() is necessary, such as when integrating with legacy code or when dealing with specific blocking dependencies.