reactor:扩展并行通量

ie3xauqp  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(368)

我有一个需要扩展的项目集合,所以我选择reactor作为它的React能力,因为扩展需要io操作。
下面是一段工作代码:

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

请注意 this::expandItem 是一个阻塞操作(多个数据库查询,一些计算…)。现在我希望这个扩展是平行的,但据我所知 .expand() 以及 .expandDeep() 只是 Flux 类而不是 ParallelFlux 班级。我试着添加 .publishOn() 以及 .subscribeOn().expand() 打电话,但运气不好。
这是我第一次使用React堆,但我没有看到任何技术问题阻止并行扩展,有什么办法吗?是api丢失了还是我丢失了什么?

31moq8wy

31moq8wy1#

是的,你是对的 ParallelFlux 没有 .expand() 以及 .expandDeep() 方法,但我可以使用其他方法,创建具有expand方法的其他发布服务器并将其传递给您的 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 并行工作。

相关问题