一. 工程准备

maven依赖:

        
            ch.qos.logback
            logback-classic
            1.4.6
        
        
            org.projectlombok
            lombok
            1.18.26
        
        
        
            io.projectreactor
            reactor-core
            3.5.5
        
        
            io.projectreactor
            reactor-test
            test
            3.5.5
        
        
            org.assertj
            assertj-core
            3.24.2
            test
        
        
            junit
            junit
            4.13.2
            test
        

简单示例:

package org.example.reactor;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import reactor.core.publisher.ConnectableFlux;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import reactor.test.StepVerifier;

import java.time.Duration;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@Slf4j
public class ReactorTest {
    @Test
    public void test() {
        // Flux支持多条数据
        Flux.just(1, 2, 3).subscribe(i -> log.debug("Flux: {}", i));

        // Mono支持0或者1条数据
        Mono.just(1).subscribe(i -> log.debug("Mono: {}", i));

        // 创建一个error的flux
        Flux.error(new Exception("error")).subscribe(
                e -> log.debug("error: {}", e),
                e -> log.error("error", e),
                () -> log.debug("Completed!")
        );

        // flux测试
        StepVerifier.create(Flux.just(1, 2)) //1
                .expectNext(1, 2) //2
                .expectComplete() //3
                .verify(); //4
    }

    @Test
    public void switchSyncToAsyncTest() {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        CountDownLatch countDownLatch = new CountDownLatch(1);

        log.debug("main thread");
        Mono.fromCallable(this::syncMethod)
                .subscribeOn(Schedulers.fromExecutor(executorService))
                .subscribe(e -> log.debug("element = {}", e), null, countDownLatch::countDown);

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    private Integer syncMethod() {

        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return 123456;
    }

    @SneakyThrows
    @Test
    public void test3() {
        final Flux source = Flux.interval(Duration.ofSeconds(1L))
                .log()
                .take(10)
                .publish()
                .autoConnect();
        source.subscribe(e -> log.debug("subscribe {}", e));
        Thread.sleep(5000);
        source.toStream().forEach(e -> log.debug("foreach {}", e));
    }

    @Test
    public void test6() {
        ConnectableFlux publish = Flux.create(fluxSink -> {
                    while (true) {
                        fluxSink.next(System.currentTimeMillis());
                    }
                })
                .log()
                .sample(Duration.ofSeconds(2))
                .publish();

        publish.subscribe(System.out::println);
        publish.connect();
    }
}
 
  

你可能感兴趣的:(反应式编程,大数据,java,数据库)