AdSense

網頁

2020/3/4

Reactor 建立Flux與Mono實例的方法 ways to create Flux and Mono instance

Reactor建立FluxMono實例的方法如下。

// Flux
Flux<String> flux1 = Flux.just("a", "b", "c");

String[] array = {"a", "b", "c"};
Flux<String> flux2 = Flux.fromArray(array);

List<String> list = Arrays.asList("a", "b", "c");
Flux<String> flux3 = Flux.fromIterable(list);

Flux<String> flux4 = Flux.fromStream(list.stream());

Flux<String> flux5 = Flux.from(flux4); // copy

Flux<String> emptyFlux = Flux.empty();

flux1.subscribe(
        value -> System.out.println(value),
        error -> System.out.println(error),
        () -> System.out.println("Complete"));
/* 印出
a
b
c
Complete
*/

// Mono
Mono<String> mono1 = Mono.just("a");

Mono<String> mono2 = Mono.from(flux1); // 取flux1的第一個元素

Mono<String> emptyMono = Mono.empty();

mono1.subscribe(
        value -> System.out.println(value),
        error -> System.out.println(error),
        () -> System.out.println("Complete"));
/* 印出
a
Complete
*/


參考:

沒有留言:

AdSense