反应堆:展开ParallelFlux

时间:2019-02-04 10:14:45

标签: java reactive-programming project-reactor

我有一个需要扩展的项目集合,所以我选择反应器是因为它的反应能力,因为扩展需要IO操作。

这是一段工作代码:

public Flux<Item> expand(List<Item> unprocessedItems) {
  return Flux.fromIterable(unprocessedItems)
    .expandDeep(this::expandItem);
}

请注意,this::expandItem是一个阻塞操作(多个数据库查询,一些计算等)。 现在,我希望这种扩展是并行的,但是据我所知.expand().expandDeep()只是Flux类的成员,而不是ParallelFlux类的成员。我尝试在.publishOn()调用之前添加.subscribeOn().expand(),但是没有运气。

这是我第一次使用Reactor,但是我看不到任何阻止并行扩展的技术问题,有什么办法可以做到吗? API丢失了还是我丢失了某些东西?

1 个答案:

答案 0 :(得分:1)

是的,ParallelFlux没有.expand().expandDeep()方法, 但是我可以使用其他方法,创建具有expand方法的其他Publisher,并将其传递给您的ParallelFlux,如下所示:

public static void main(String[] args) {      

    Function<Node, Flux<Node>> expander =
        node -> Flux.fromIterable(node.children);

    List<Node> roots = createTestNodes();

    Flux.fromIterable(roots)
        .parallel(4)
        .runOn(Schedulers.parallel())
        .flatMap(node -> Flux.just(node).expandDeep(expander))
        .doOnNext(i -> System.out.println("Time: " + System.currentTimeMillis() + " thread: " + Thread.currentThread().getName() + " value: " + i))
        .sequential()
        .subscribe();

    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("finished");

}

我的测试数据:

static final class Node {
    final String name;
    final List<Node> children;

    Node(String name, Node... nodes) {
        this.name = name;
        this.children = new ArrayList<>();
        children.addAll(Arrays.asList(nodes));
    }

    @Override
    public String toString() {
        return name;
    }
}

static List<Node> createTestNodes() {
    return new Node("root",
        new Node("1",
            new Node("11")
        ),
        new Node("2",
            new Node("21"),
            new Node("22",
                new Node("221")
            )
        ),
        new Node("3",
            new Node("31"),
            new Node("32",
                new Node("321")
            ),
            new Node("33",
                new Node("331"),
                new Node("332",
                    new Node("3321")
                )
            )
        ),
        new Node("4",
            new Node("41"),
            new Node("42",
                new Node("421")
            ),
            new Node("43",
                new Node("431"),
                new Node("432",
                    new Node("4321")
                )
            ),
            new Node("44",
                new Node("441"),
                new Node("442",
                    new Node("4421")
                ),
                new Node("443",
                    new Node("4431"),
                    new Node("4432")
                )
            )
        )
    ).children;
}

结果:

Time: 1549296674522 thread: parallel-4 value: 4
Time: 1549296674523 thread: parallel-4 value: 41
Time: 1549296674523 thread: parallel-2 value: 2
Time: 1549296674523 thread: parallel-2 value: 21
Time: 1549296674523 thread: parallel-3 value: 3
Time: 1549296674523 thread: parallel-3 value: 31
Time: 1549296674523 thread: parallel-1 value: 1
Time: 1549296674523 thread: parallel-1 value: 11
Time: 1549296674525 thread: parallel-2 value: 22
Time: 1549296674525 thread: parallel-2 value: 221
Time: 1549296674526 thread: parallel-3 value: 32
Time: 1549296674526 thread: parallel-3 value: 321
Time: 1549296674526 thread: parallel-3 value: 33
Time: 1549296674526 thread: parallel-3 value: 331
Time: 1549296674526 thread: parallel-3 value: 332
Time: 1549296674526 thread: parallel-3 value: 3321
Time: 1549296674526 thread: parallel-4 value: 42
Time: 1549296674526 thread: parallel-4 value: 421
Time: 1549296674526 thread: parallel-4 value: 43
Time: 1549296674526 thread: parallel-4 value: 431
Time: 1549296674526 thread: parallel-4 value: 432
Time: 1549296674526 thread: parallel-4 value: 4321
Time: 1549296674527 thread: parallel-4 value: 44
Time: 1549296674527 thread: parallel-4 value: 441
Time: 1549296674527 thread: parallel-4 value: 442
Time: 1549296674527 thread: parallel-4 value: 4421
Time: 1549296674528 thread: parallel-4 value: 443
Time: 1549296674528 thread: parallel-4 value: 4431
Time: 1549296674528 thread: parallel-4 value: 4432

您可以看到expander在并行线程中工作。