首页 > 解决方案 > 读取 Part Flux 时传递状态

问题描述

我有一个使用 Webflux 的 Boot 2.x 应用程序,我正在Parts使用@RequestBody Flux<Part>.

我的问题是我需要读取第一个Part,使用其中的内容初始化一个对象Part,然后传递该对象以在第二个中使用Part。在确保阅读可用的每个部分的同时,我该如何做呢?

我目前的解决方案是使用groupBy,但这样做会触发等到所有部分完成,这是不可接受的。

这是我正在尝试做的一个例子:

parts.scan( new Foo(), (foo, part) -> {
    if(part.name().equalsIgnoreCase("first_part"))
    {
        Jackson2JsonDecoder jackson2JsonDecoder = new Jackson2JsonDecoder();
        Mono<Metadata> metadata = jackson2JsonDecoder.decodeToMono(part.content(), ResolvableType.forClass(Metadata.class), null, null);
        // Here's my problem. How do I call foo.init(metadata) in a non-blocking way while still returning foo so it can be used by the next part?
    }
    else if(part.name().equalsIgnoreCase("second_part"))
    {
        // Use initialized foo from part 1 to push second_part's DataBuffer Flux
    }
});

谢谢!

标签: spring-webfluxproject-reactor

解决方案


推荐阅读