Project reactor: collectList() doesn't work for Flux.create()

岁酱吖の 提交于 2020-01-11 11:34:54

问题


Below example prints integers from 1 to 10 and a list of (7, 8, 9, 10)

public void streamCollect() {

    ConnectableFlux<Integer> connect = Flux.range(1, 10)
            .publish();

    connect.subscribe(v -> System.out.println("1: " + v));

    connect
            .filter(number -> number > 6)
            .collectList()
            .subscribe(v -> System.out.println("4: " + v));

    connect.connect();
}

Result:

1: 1

1: 2

1: 3

1: 4

1: 5

1: 6

1: 7

1: 8

1: 9

1: 10

4: [7, 8, 9, 10]

Next example should produce the same result but instead prints out only numbers from 1 to 10 but no list. Why?

public void streamCollect() {

    ConnectableFlux<Integer> connect = Flux.<Integer>create(emitter -> {

        Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
                .forEach(t -> emitter.next(t));
    }).publish();

    connect.subscribe(v -> System.out.println("1: " + v));

    connect
            .filter(number -> number > 6)
            .collectList()
            .subscribe(v -> System.out.println("4: " + v));

    connect.connect();
}

Result:

1: 1

1: 2

1: 3

1: 4

1: 5

1: 6

1: 7

1: 8

1: 9

1: 10


回答1:


The collectList waits for the onComplete signal, which you never produce in your create lambda



来源:https://stackoverflow.com/questions/49732852/project-reactor-collectlist-doesnt-work-for-flux-create

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!