how to combine two publisher in one in spring reactor

时间:2017-08-13 14:04:06

标签: project-reactor spring-webflux

I have implemented a dummy reactive repository but I am struggling with the update method:

@Override
public Mono<User> updateUser(int id, Mono<User> updateMono) {
    return  //todo with getUser
}

@Override
public Mono<User> getUser(int id) {
    return Mono.justOrEmpty(this.users.get(id));
}

From one hand I have incoming publisher Mono<User> updateMono, from the other hand I have another publisher during Mono.justOrEmpty(this.users.get(id)).

How to combine it together, make update, and give back just one publisher?

The only thing come to my mind is:

@Override
public Mono<User> updateUser(int id, Mono<User> updateMono) {
    return  getUser(id).doOnNext(user -> {
        updateMono.subscribe(update -> {
            users.put(id, new User(id, update.getName(), update.getAge()));
            System.out.format("Updated user with id %d to %s%n", id, update);
        });
    });
}

Is it correct?

2 个答案:

答案 0 :(得分:2)

我最近玩过Spring 5 Reactive Streams功能,并写下了some sample codes(尚未通过博客或Twitter公开,我还需要在Reactor上练习更多)。

我遇到了同样的问题,最后使用Mono.zip来更新MongoDB中的现有项目。

https://github.com/hantsy/spring-reactive-sample/blob/master/boot-routes/src/main/java/com/example/demo/DemoApplication.java

public Mono<ServerResponse> update(ServerRequest req) {

    return Mono
        .zip(
            (data) -> {
                Post p = (Post) data[0];
                Post p2 = (Post) data[1];
                p.setTitle(p2.getTitle());
                p.setContent(p2.getContent());
                return p;
            },
            this.posts.findById(req.pathVariable("id")),
            req.bodyToMono(Post.class)
        )
        .cast(Post.class)
        .flatMap(post -> this.posts.save(post))
        .flatMap(post -> ServerResponse.noContent().build());

}

更新:用Kotlin编写的另一个工作版本。

fun update(req: ServerRequest): Mono<ServerResponse> {
    return this.posts.findById(req.pathVariable("id"))
            .and(req.bodyToMono(Post::class.java))
            .map { it.t1.copy(title = it.t2.title, content = it.t2.content)    }
            .flatMap { this.posts.save(it) }
            .flatMap { noContent().build() }
}

答案 1 :(得分:1)

请参阅the reference guide on finding the right operator

值得注意的是,对于Mono,您有andwhenthen(注意最后一个将在3.1.0中成为flatMap,而flatmap将成为flatMapMany

doOnNext更适用于日志记录或统计信息收集等边操作。订阅内部订阅是另一种不好的形式;通常你想要flatMap或类似的。