java:reactor的Mono和Reactor的简单例子

发布于:2024-06-11 ⋅ 阅读:(78) ⋅ 点赞:(0)

【pom.xml】

<dependency>
    <groupId>io.projectreactor</groupId>
    <artifactId>reactor-core</artifactId>
    <version>3.3.17.RELEASE</version>
</dependency>

【MyReactorTest2.java】

package com.chz.myReactor;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public class MyReactorTest2  {

    public static void main(String[] args) {
        System.out.println("--------------------------------");

        Mono<Integer> mono = Mono.just(1);
        mono.subscribe(
                value -> System.out.println("Received: " + value),
                error -> System.err.println("Error: " + error),
                () -> System.out.println("Completed")
        );
        System.out.println("--------------------------------");

        Flux<Integer> flux = Flux.just(1, 2, 3, 4, 5);
        flux.subscribe(
                value -> System.out.println("Received: " + value),
                error -> System.err.println("Error: " + error),
                () -> System.out.println("Completed")
        );
        System.out.println("--------------------------------");

        Flux<Integer> flux2 = flux.map(value -> value * 2);
        flux2.subscribe(
                value -> System.out.println("Received: " + value),
                error -> System.err.println("Error: " + error),
                () -> System.out.println("Completed")
        );
        System.out.println("--------------------------------");
    }
}

运行【MyReactorTest2】
在这里插入图片描述