how to combine two publisher in one in spring reactor

Viewed 16450

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 Answers
Related