首页 > 其他分享 >Reactor接口之四

Reactor接口之四

时间:2023-05-07 15:55:25浏览次数:36  
标签:10 Reactor void 接口 Flux Test countDownLatch 之四 println

interval

@Test
public void testInterval() {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    Flux.range(1,10)
            .zipWith(Flux.interval(Duration.ofSeconds(1)))
            .subscribe(System.out::println, null ,countDownLatch::countDown);

    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

interval定时生成一个Flux。输出:

[1,0]
[2,1]
[3,2]
[4,3]
[5,4]
[6,5]
[7,6]
[8,7]
[9,8]
[10,9]

 

@Test
public void testInterval1() {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    Flux.interval(Duration.ofSeconds(1))
            .zipWith(Flux.range(1,10))
            .subscribe(System.out::println, null ,countDownLatch::countDown);

    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

每个1秒输出一个Flux。每个1秒输出一组数据:

[0,1]
[1,2]
[2,3]
[3,4]
[4,5]
[5,6]
[6,7]
[7,8]
[8,9]
[9,10]

 

@Test
public void testInterval2() {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    Flux.interval(Duration.ofSeconds(1), Duration.ofMillis(200))
            .zipWith(Flux.range(1,10))
            .subscribe(System.out::println, null ,countDownLatch::countDown);

    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

和上面类似。Flux.interval第一个参数是第一次调用时延迟时间,第二个参数是定时隔间时间。

sort

@Test
public void testSort() {
    CountDownLatch countDownLatch = new CountDownLatch(1);
    Flux.create(t -> {
                for (int i = 0; i < 10; i++) {
                    t.next(random.nextInt(100));
                }
                t.complete();
            })
            .sort()
            .subscribe(System.out::println,null, countDownLatch::countDown);

    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

排序。

all

@Test
public void testAll() {
    Flux.create(t -> {
                for (int i = 0; i < 10; i++) {
                    t.next(random.nextInt(100));
                }
                t.complete();
            })
            .all(k -> (int)k > 50)
            .subscribe(System.out::println);
}

all表示所有元素都满足条件则返回true,否则返回false。

any

@Test
public void testAny() {
    Flux.create(t -> {
                for (int i = 0; i < 10; i++) {
                    t.next(random.nextInt(100));
                }
                t.complete();
            })
            .any(k -> (int)k > 50)
            .subscribe(System.out::println);
}

any表示有一个元素返回true则返回true,否则返回false。

buffer

@Test
public void testBuffer() {
    Flux.range(1,10)
            .buffer()
            .subscribe(System.out::println);
}

buffer将所有传入的值收集到单个List缓冲区中,该缓冲区将在该Flux完成后由返回的Flux发出。

cache

@Test
public void testCache() {
    Flux.range(1,10)
            .cache()
            .subscribe(System.out::println);
}

cache将此Flux转换为一个热点源,并缓存最后发出的信号以供下一个Subscriber使用。将保留无限量的onNext信号。完成和错误也将被重播。

collectSortedList

@Test
public void testClooectSortedList() {
    Flux.create(t -> {
                for (int i = 0; i < 10; i++) {
                    t.next(random.nextInt(100));
                }
                t.complete();
            })
            .collectSortedList()
            .subscribe(System.out::println);
}

collectSortedList排序并组合成List。

标签:10,Reactor,void,接口,Flux,Test,countDownLatch,之四,println
From: https://www.cnblogs.com/shigongp/p/17379371.html

相关文章

  • Reactor接口之三
    defer@TestpublicvoidtestDefer(){Flux.defer(()->{returnFlux.range(0,10);}).subscribe(System.out::println);}输出0到9。defer每次对结果Flux进行订阅时,懒惰地提供发布服务。因此实际的源实例化会推迟到每次订阅时。collect@Te......
  • SpringCloud gateway内置过滤器之四
    1、RewriteLocationResponseHeaderGatewayFilterRewriteLocationResponseHeaderGatewayFilter修改Location响应标头的值,通常是为了消除后端特定的详细信息。有stripVersionMode、locationHeaderName、hostValue和protocolsRegex参数。protocolsRegx参数必须是有效的正则表达式......
  • 接口自动化 测试数据驱动 DDD模块使用
    一、DDT简单介绍名称:Data-DrivenTests,数据驱动测试作用:由外部数据集合来驱动测试用例的执行核心的思想:数据和测试代码分离应用场景:一组外部数据来执行相同的操作优点:当测试数据发生大量变化的情况下,测试代码可以保持不变实际项目:excel存储测试数据,ddt读取测试数据到单元......
  • vue配置请求本地接口
    proxy:{'/nrms':{target:'http://localhost:8921/',changeOrigin:true,ws:true,pathRewrite:{'^/nrms':'/'}},'/':{targ......
  • Reactor接口之二
    merge@TestpublicvoidtestMerge(){Flux.merge(Flux.just(1,2,3),Flux.range(5,6)).subscribe(System.out::println);}merge将多个Flux合并成一个Flux。 @TestpublicvoidtestMerge1(){Flux.range(1,5).mergeWith(Flux.just(8,9......
  • 【接口自动化测试】月薪12k必会技术,从0到1学习接口自动化测试,6个操作安排的明明白白
        ​导读:在所有的开发测试中,接口测试是必不可少的一项。有效且覆盖完整的接口测试,不仅能保障新功能的开发质量,还能让开发在修改功能逻辑的时候有回归的能力,同时也是能优雅地进行重构的前提。编写接口测试要遵守哪些原则?测试代码的结构应该是什么样的?接口测试......
  • 聊聊关于,SpringBoot写后端接口
    前言:一个后端接口大致分为四个部分组成:接口地址(url)、接口请求方式(get、post等)、请求数据(request)、响应数据(response)。如何构建这几个部分每个公司要求都不同,没有什么“一定是最好的”标准,但一个优秀的后端接口和一个糟糕的后端接口对比起来差异还是蛮大的,其中最重要的关键点就是......
  • Java获取pdd详情api接口、商品详情、商品描述、宝贝链接获取展示示例
    ​拼多多商品详情就是对拼多多商城中的宝贝的描述了,消费者们在进入到宝贝的详情页面后,可以通过这些描述去了解该款宝贝。其实这样也可以大大的增加商品的转化率。那么它的作用有什么呢?1.突出商品卖点:把商品的特色和突出点写表现出来,很更好的吸引到顾客,让顾客有点击和购买的欲......
  • 与chatGPT谈TyptScript接口问题
    与chatGPT谈TyptScript接口问题问1:能给我说说c#中的inteface与typescript中的inteface的不同与相同吗?答1:C#中的Interface和TypeScript中的Interface有一些相似之处,但也有一些不同之处。相同点:Interface都是用来定义对象的结构和行为的。都支持继承和多态的概念。......
  • 具有前后按钮切换+头图切换联动通用接口(应付不同的联动需要)的图片滑动效果
    -------scroll_tween.js----------CHRD.scroll.tween=function(){varTween={Quart:{easeOut:function(t,b,c,d){return-c*((t=t/d-1)*t*t*t-1)+b;}},Back:{easeOut......