Reactor系列(三)创建Flux,Mono(续)

创建Mono

视频讲解:https://www.bilibili.com/video/av78944069/

FluxMonoTestCase.java

package com.example.reactor;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Random;
import java.util.concurrent.CompletableFuture;

@Slf4j
public class FluxMonoTestCase extends BaseTestCase {
    @Test
    public void createMono(){
        //字符串
        Mono<String> stringMono = Mono.just("Hello World");
        //Callable创建
        Mono<String> stringMono1 = Mono.fromCallable(()->
        {
            return "Hello World";
        });
        //Future创建
        Mono<String> stringMono2 = Mono.fromFuture(CompletableFuture.completedFuture("Hello World"));
        Random random = new Random();
        //Suppier创建
        Mono<Double> doubleMono = Mono.fromSupplier(random::nextDouble);
        //Mono创建
        Mono<Double> doubleMono1 = Mono.from(doubleMono);
        //Flux创建
        Mono<Integer> integerMono = Mono.from(Flux.range(1,5));
        integerMono.subscribe(System.out::println);
        stringMono2.subscribe(System.out::println);
    }
}

BaseTestCase.java

package com.example.reactor;

import java.util.Arrays;
import java.util.List;

public class BaseTestCase {
    protected static final List<Employee> list = Arrays.asList(
            new Employee(1, "Alex", 1000),
            new Employee(2, "Michael", 2000),
            new Employee(3, "Jack", 1500),
            new Employee(4, "Owen", 1500),
            new Employee(5, "Denny", 2000));
}

关注公众号,坚持每天3分钟视频学习

阅读 347

推荐阅读
目录