欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-07-14 16:44:46
...

创建Mono

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

 

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

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分钟视频学习

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