ํ์์ IoT๋ฅผ ๋น๋๊ธฐ์ ์ผ๋ก ์ฒ๋ฆฌํ๊ธฐ ์ํด WebFlux๋ฅผ ์ฌ์ฉํ๊ณ ์๋ค.
WebFlux์ ๋ํด ์๊ฒ๋ง ์๊ณ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ์ด๋ฒ ๊ธฐํ์ ๊ณต๋ถํด๋ณด๊ธฐ๋ก ํ๋ค.
โด๏ธ Sync(๋๊ธฐ)์ Async(๋น๋๊ธฐ)
๐ถ Sync : ํด๋ผ์ด์ธํธ์์ ๋ฉ์๋ ํธ์ถ → ๋ฉ์๋ ์ ๊ณตํ๋ ๊ณณ์์ ๊ธฐ๋ฅ์ ๋ชจ๋ ์ํํด ๊ฒฐ๊ณผ๊ฐ์ด ๊ฒฐ์ ๋๋ฉด ๊ทธ๋ ๋ฐํ
๐ ์์ฒญํ ์์ ์ ๋ํด ์๋ฃ ์ฌ๋ถ๋ฅผ ์ ๊ฒฝ ์จ์ ์์ ์ ์์ฐจ์ ์ผ๋ก ์ํํ ์ง ์๋์ง ๊ฒฐ์
๐ถ Async : ๊ฒฐ๊ณผ๊ฐ์ด ๊ฒฐ์ ๋๊ธฐ ์ ์ ์ผ๋จ ๋ฐํ (์์ ๋ณด์ฅ X)
๐ ์์ฒญํ ์์ ์ ๋ํด ์๋ฃ ์ฌ๋ถ๋ฅผ ๋ฐ์ง์ง ์๊ณ ์์ ์ ๋ค์ ์์ ์ ๊ทธ๋๋ก ์ํ
โด๏ธ Blocking๊ณผ Non-Blocking
๐ท Blocking : ์์ฒญํ ์์ ์ด ๋๋ ๋๊น์ง ๋ค๋ฅธ ์์ ํ์ง ์๊ณ ๊ธฐ๋ค๋ฆผ
๐ท Non-Blocking : ์์ฒญํ ์์ ์ด ์ํ๋๋ ๋์ ๋ค๋ฅธ ์์ ๊ฐ๋ฅ.
โด๏ธ ์ฝ๋ฐฑํจ์
๐ ๋ค๋ฅธ ์์ ์ ์๋ฃ ์ฌ๋ถ๋ ๊ฒฐ๊ณผ์ ๋ํ ํ์ฒ๋ฆฌ๋ฅผ ์ํด ์ด์ฉ
CompletableFuture ๋?
java.util.concurrent ํจํค์ง์ ์ํ๋ ํด๋์ค ์ค ํ๋๋ก, ๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ์ ๋ณด๋ค ์ฝ๊ฒ ๊ตฌํํ ์ ์๋๋ก ๋์์ค๋ค.
Java 8์ ์ฒ์ ๋์ ๋์๊ณ , Future์ CompletionStage์ ๊ตฌํํ๊ณ ์๋ค.
public class CompletableFuture<T> implements Future<T>, CompletionStage<T>
Future์ CompletionStage๋ ๋ญ๊น.
Future
- Java 5์ ๋์
- ๋น๋๊ธฐ์ ์ธ ์์ ์ํ
- ์์ ์ํ ํ์ธ - ์๋ฃ, ์ทจ์, ์งํ ์ค ์ฌ๋ถ
- ํด๋น ์์ ์ด ์๋ฃ๋๋ฉด ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋ ์ธํฐํ์ด์ค
๐ถ ์ฃผ์ ๋ฉ์๋
public interface Future<V> {
boolean cancel(boolean mayInterruptIfRunning);
boolean isCancelled();
boolean isDone();
V get() throws InterruptedException, ExecutionException;
V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;
}
- cancel : ์์ ์ทจ์ ๐ mayInterruptIfRunning์ด true์ด๋ฉด ์คํ ์ค์ธ ์์ ์ค๋จ
- get() : ์์ ์ด ์๋ฃ๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฐ ํ ๊ฒฐ๊ณผ ๋ฐํ ๐ ์์ ์ด ์๋ฃ๋ ๋๊น์ง ํธ์ถ ์ค๋ ๋๋ ๋ธ๋กํน๋จ
๐ถ ํ๊ณ
- cancel์ ์ ์ธํ๊ณ ์ธ๋ถ์์ future ์ปจํธ๋กค X ๐ ์ฝ๋ฐฑ ๋ฏธ์ง์
- ๋ธ๋กํน : Future.get() ๋ฉ์๋๋ ๊ฒฐ๊ณผ๊ฐ ์ค๋น๋ ๋๊น์ง ๋ธ๋กํน๋๋ฏ๋ก ๋น๋๊ธฐ ์์ ์ ์ด์ ์ถฉ๋ถํ ํ์ฉ X
- ๋ฐํ๋ ๊ฒฐ๊ณผ๋ฅผ get()ํด์ ์ ๊ทผํ๊ธฐ ๋๋ฌธ์ ๋น๋๊ธฐ ์ฒ๋ฆฌ ์ด๋ ค์
- ์๋ฃ๋๊ฑฐ๋ ์๋ฌ ๋ฐ์ํ๋์ง ๊ตฌ๋ถ ์ด๋ ค์
- ์กฐํฉ ์ด๋ ค์ : ์ฌ๋ฌ ๋น๋๊ธฐ ์์ ์ ์กฐํฉ, ์์ฐจ์ ์ผ๋ก ์คํ, ๊ฒฐ๊ณผ ์กฐํฉ ์ด๋ ค์
CompletionStage
- Java 8์ ๋์
- ๋น๋๊ธฐ์ ์ธ ์์ ์ํ
- ํด๋น ์์ ์ด ์๋ฃ๋๋ฉด ๊ฒฐ๊ณผ๋ฅผ ์ฒ๋ฆฌํ๊ฑฐ๋ ๋ค๋ฅธ CompletionStage๋ฅผ ์ฐ๊ฒฐํ๋ ์ธํฐํ์ด์ค
- ๋น๋๊ธฐ task๋ค์ ์คํํ๊ณ ๊ฐ์ ๋ณํํ๋ ๋ฑ chaining์ ์ด์ฉํ ์กฐํฉ ๊ฐ๋ฅ
- ์๋ฌ๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํ ์ฝ๋ฐฑ ์ ๊ณต
๐ถ ์ฃผ์ ๋ฉ์๋
public interface CompletionStage<T> {
public <U> CompletionStage<U> thenApply(Function<? super T,? extends U> fn);
public <U> CompletionStage<U> thenApplyAsync(Function<? super T,? extends U> fn);
public CompletionStage<Void> thenAccept(Consumer<? super T> action);
public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action);
public CompletionStage<Void> thenRun(Runnable action);
public CompletionStage<Void> thenRunAsync(Runnable action);
public <U> CompletionStage<U> thenCompose(Function<? super T, ? extends CompletionStage<U>> fn);
public <U> CompletionStage<U> thenComposeAsync(Function<? super T, ? extends CompletionStage<U>> fn);
public CompletionStage<T> exceptionally(Function<Throwable, ? extends T> fn);
}
- thenAccept[Async]
- Consumer๋ฅผ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์
- ํ์ฌ ๋จ๊ณ์ ๊ฒฐ๊ณผ๋ฅผ ์ฌ์ฉํ์ฌ ๋๊ธฐ์ ์ผ๋ก ์์ ์ํํ๊ณ ๋ฐํ X
- ๊ฐ์ ๋ฐ์์ action๋ง ์ํํ๋ ๊ฒฝ์ฐ์ ์ ์ฉ
- thenApply[Async]
- Function์ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์
- ์ด์ task๋ก๋ถํฐ T ํ์ ์ ๊ฐ์ ๋ฐ์์ ๊ฐ๊ณตํ๊ณ U ํ์ ์ ๊ฐ ๋ฐํ
- ๋ค์ task์๊ฒ ๋ฐํํ๋ ๊ฐ์ด ์ ๋ฌ๋จ
- thenApply → ํ์ฌ CompletableFuture๋ฅผ ์๋ฃํ ๋์ผํ ์ค๋ ๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ณํ ํจ์ ์คํ
๐ ๋ณํ ํจ์์ ์คํ์ ๊ฒฐ๊ณผ๊ฐ ์ฌ์ฉ ๊ฐ๋ฅํด์ง ์งํ์ ๋ฐ์ - CompletableFuture์ ๊ฒฐ๊ณผ์ ๋ํด ์ฒด์ด๋ ์์ ์ํ
- ๊ฐ์ ๋ณํํด์ ์ ๋ฌํด์ผ ํ๋ ๊ฒฝ์ฐ ์ ์ฉ
- thenCompose[Async]
- Fuction์ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์
- ์ด์ task๋ก๋ถํฐ T ํ์ ์ ๊ฐ์ ๋ฐ์์ ๊ฐ๊ณตํ๊ณ U ํ์ ์ CompletionStage ๋ฐํ
- ๋ฐํํ CompletionStage๊ฐ done ์ํ๊ฐ ๋๋ฉด ๊ฐ์ ๋ค์ task์ ์ ๋ฌ
- ๋ค๋ฅธ future๋ฅผ ๋ฐํํด์ผํ๋ ๊ฒฝ์ฐ ์ ์ฉ
- thenRun[Async]
- Runnable์ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์
- ์ด์ task๋ก๋ถํฐ ๊ฐ์ ๋ฐ์ง ์๊ณ ๊ฐ์ ๋ฐํ x
- ๋ค์ task์๊ฒ null์ด ์ ๋ฌ๋จ
- future๊ฐ ์๋ฃ๋์๋ค๋ ์ด๋ฒคํธ๋ฅผ ๊ธฐ๋กํ ๋ ์ ์ฉ
- then*[Async]
- ๋น๋๊ธฐ์ ์ผ๋ก ์คํ
- Async ๐ ๋ณ๋์ ์ค๋ ๋์์ ์คํ
- ํธ์ถ๋ ์ค๋ ๋๋ ์ฆ์ ๋ฐํ๋๊ณ ๊ฒฐ๊ณผ๋ ๋์ค์ ์ฌ์ฉ ๊ฐ๋ฅ
- Executor ์ฌ์ฉํด์ ์ค๋ ๋ ํ์ ํตํด ์ค๋ ๋ ๊ด๋ฆฌ ๊ฐ๋ฅ
- exceptionally
- Function์ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์
- ์ด์ task์์ ๋ฐ์ํ exception์ ๋ฐ์์ ์ฒ๋ฆฌํ๊ณ ๊ฐ ๋ฐํ
- ๋ค์ task์๊ฒ ๋ฐํ๋ ๊ฐ์ ์ ๋ฌ
- future ํ์ดํ์์ ๋ฐ์ํ ์๋ฌ๋ฅผ ์ฒ๋ฆฌํ ๋ ์ ์ฉ
CompletableFuture
public class CompletableFuture<T> implements Future<T>, CompletionStage<T> {
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) { … }
public static CompletableFuture<Void> runAsync(Runnable runnable) { … }
public boolean complete(T value) { … }
public boolean isCompletedExceptionally() { … }
public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs) { … }
public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs) { … }
}
- supplyAsync
- ๋น๋๊ธฐ์ ์ผ๋ก ๊ฐ์ ๊ณ์ฐํ๊ณ ๊ทธ ๊ฐ์ ๋ฐํ
- Supplier๋ฅผ ์ ๊ณตํ์ฌ CompletableFuture ์์ฑ ๊ฐ๋ฅ
- Supplier์ ๋ฐํ๊ฐ์ด CompletableFuture์ ๊ฒฐ๊ณผ๋ก
- runAsync
- ๋น๋๊ธฐ์ ์ผ๋ก ์คํ๋์ง๋ง ๋ฐํ๊ฐ์ด ์๋ ์์ ์คํ
- Runnable ์ ๊ณตํ์ฌ CompletableFuture ์์ฑ ๊ฐ๋ฅ
- ๋ค์ task์ null ์ ๋ฌ๋จ
๐ท Supplier ์ธํฐํ์ด์ค
@FunctionalInterface public interface Supplier<T> { T get(); }โ
๋งค๊ฐ๋ณ์ ์์ด ์ด๋ค ๊ฐ์ ์ ๊ณตํ๋ ๋ฉ์๋๋ฅผ ์ ์ํ๋ ํจ์ํ ์ธํฐํ์ด์ค
๐ ๊ฐ์ ์์ฑํ๊ฑฐ๋ ๊ณ์ฐํ๋ ๋ฐ ์ฌ์ฉ๋จ
๐ท Runnable ์ธํฐํ์ด์ค
@FunctionalInterface public interface Runnable { void run(); }โ
๋งค๊ฐ๋ณ์๊ฐ ์๊ณ ๋ฐํ๊ฐ์ด ์๋ ์์ ์ ์ ์ํ๋ ํจ์ํ ์ธํฐํ์ด์ค
๐ ์์ ์ ์คํํ๋ ๋ฐ ์ฌ์ฉ๋จ
- complete
- complete์ ์ํด์ ์ํ๊ฐ ๋ฐ๋์๋ค๋ฉด true, ์๋๋ผ๋ฉด false ๋ฐํ
- isCompletedExceptionally
- CompletableFuture ๊ฐ์ฒด๊ฐ ์๋ฃ๋ ์ํ
- exception์ ์ํด์ complete ๋์๋์ง ํ์ธ
- ์๋ฃ๋ ๊ฒฐ๊ณผ๊ฐ ์์ธ๋ฅผ ๋์ง๋ฉด true ๋ฐํ, ์๋๋ฉด false ๋ฐํ
- ์์ธ๋ฅผ ๋์ง์ง ์๊ณ ์ ์์ ์ผ๋ก ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ์ ๊ฒฝ์ฐ false ๋ฐํ
- ๋น๋๊ธฐ ์์ ์ด ์์ธ๋ฅผ ๋์ง๋์ง ์ฌ๋ถ ํ์ธํ ๋ ์ ์ฉํ๊ฒ ์ฌ์ฉ๋จ
- allOf
- ์ฌ๋ฌ completableFuture๋ฅผ ๋ชจ์์ ํ๋๋ก ๋ณํ
- ๋ชจ๋ completableFuture๊ฐ ์๋ฃ๋๋ฉด ์ํ๊ฐ done์ผ๋ก ๋ณ๊ฒฝ
- void ๋ฐํํ๋ฏ๋ก ๊ฐ๊ฐ์ ๊ฐ์ get์ผ๋ก ์ ๊ทผ
- anyOf
- ์ฌ๋ฌ completableFuture๋ฅผ ๋ชจ์์ ํ๋๋ก ๋ณํ
- ์ฃผ์ด์ง future ์ค ํ๋๋ผ๋ ์๋ฃ๋๋ฉด ์ํ๊ฐ done์ผ๋ก ๋ณ๊ฒฝ
- ์ ์ผ ๋จผ์ done ์ํ๊ฐ ๋๋ future์ ๊ฐ ๋ฐํ
๐ถ ํ๊ณ
- ์ง์ฐ ๋ก๋ฉ ๊ธฐ๋ฅ ์ ๊ณต X
- ์ง์์ ์ผ๋ก ์์ฑ๋๋ ๋ฐ์ดํฐ ์ฒ๋ฆฌํ๊ธฐ ์ด๋ ค์
์ฐธ๊ณ ์๋ฃ
https://www.baeldung.com/java-completablefuture
https://11st-tech.github.io/2024/01/04/completablefuture/
'Spring > Spring WebFlux' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๐ Reactive] 1. Reactive ์์คํ ๊ณผ Reactive ํ๋ก๊ทธ๋๋ฐ (0) | 2024.12.16 |
---|---|
[WebFlux] Reactor ์ฐ์ฐ์ ์ ๋ฆฌ (0) | 2024.07.17 |
[WebFlux] flatMap๊ณผ map์ ์ฐจ์ด (0) | 2024.07.17 |
[WebFlux] Reactive Streams (0) | 2024.07.03 |
[WebFlux] Spring WebFlux๋? (0) | 2024.01.13 |