Reactor系列(十六)Disposable停止Flux流

java#reactor#flux#disposable

停止flux流

视频讲解: https://www.bilibili.com/vide...

FluxMonoTestCase.java

package com.example.reactor;

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

import java.time.Duration;

@Slf4j
public class FluxMonoTestCase extends BaseTestCase {
    @Test
    public void disposable() throws InterruptedException {
        Flux<Long> longFlux =Flux.interval(Duration.ofMillis(1));
        //take方法准确获取订阅数据量
        Disposable disposable = longFlux.take(50).subscribe(x->log.info("->{}",x));
        //不能停止正在推送数据中的Flux或Mono流
        Thread.sleep(100);
        //彻底停止正在推送数据中的Flux或Mono流
        disposable.dispose();
        log.info("->Stop");
    }
}

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

阅读 310

推荐阅读
目录