4、响应式编程

官网翻译

概念

背压机制

在生产者和消费者模型中,消费者除了消费生产者的数据之外,还需要一种向生产者反馈流量需求的机制,这个机制就是背压机制

Flux对象的创建

  @Test
    public void testCreate(){
     
        Integer[] tem = new Integer[5];
        Flux.fromArray(tem);
//        Flux.interval(Duration.of(10, TemporalUnit));
        Duration duration = Duration.ofDays(1);
        System.out.println(60 * 60 * 24);
        System.out.println(duration.getSeconds());
//        LocalDate.
//        使用just产生 Create a Flux that emits the provided elements and then completes.
        Flux.just(1,2,3,4);
//        产生从1开始的,数量为10的序列 Flux序列
        Flux<Integer> range = Flux.range(1, 10);
        range.doOnNext(System.out::println).subscribe();
//        Programmatically create a Flux by generating signals one-by-one via a consumer callback.
//        动态产生 generate() 方法

        System.out.println("测试generate,最多只能调用一次next");
        Flux<Integer> generate = Flux.generate((sink) -> {
     
            sink.next(100);
//            必须要有这个complete方法,表示结束
            sink.complete();
        });
        generate.doOnNext(System.out::print).subscribe();

        System.out.println("测试create 可以多次调用next");
        Flux.create((sink)->{
     
            for (int i = 0; i < 30; i++) {
     
                sink.next(i);
            }
            sink.complete();
        }).doOnNext(System.out::print).subscribe();

    }

消费:

 @Test
    public void midoperate() {
     
        Flux<Integer> objectFlux = Flux.create((sink) -> {
     
                    for (int i = 0; i < 5; i++) {
     
                        sink.next(i);
                    }
                }
        );
//        需要注意的是buffe中的cap一定要有,默认是Integer.MAX_VALUE,不够的话就不打印,总共5个数字,这里就打印了第一组 0,1,2其余的就丢失了 。。。
        Flux<List<Integer>> buffer = objectFlux.buffer(2);
        buffer.subscribe(System.out::println);
        System.out.println("\n++++++++++++++++++");
        print(objectFlux.map(x -> x + 100));
        System.out.println("\n++++++++++++++++++");
//        将其转成一个流,然后再合并
        print(objectFlux.flatMap(x -> Flux.just(x, 1, 2, 4)));
        System.out.println("\n+++++++++++++++++++++++ startWith");
//        以什么开头
        System.out.println("以什么开头");
        print(objectFlux.startWith(1,2,3));
        System.out.println("\n ++++++++++++++");
        Flux.just(1,2,3).concatWith(Flux.just(2,3,4)).subscribe(System.out::println);
        System.out.println("++++++++++++++");
        Mono.just(11).concatWith(Flux.just(12,3)).subscribe(System.out::println);
//concatWith好像对于create创建的不生效,这里只有just才生效,concatWith在末尾加入,这里封装了一层,这样就可以加入了
        System.out.println("++++++++++++");
        Flux.just(objectFlux).concatWith(Flux.just(Flux.just(1,2,33))).subscribe((x) -> {
     
            x.subscribe(System.out::println);
        });
        System.out.println("+++++++++++");
        objectFlux.concatWith(Flux.just(1,2,33)).subscribe(System.out::println);
    }

你可能感兴趣的:(实习)