Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

java - 如何合并项目反应堆中两个反应性流的数据?(How to combine data from two reactive streams in project reactor?)

I started using Project Reactor recently and I can't work out how to work with nested streams.

(我最近开始使用Project Reactor,但无法弄清楚如何使用嵌套流。)

I want to update data of outer Mono with some data of inner Mono.

(我想用内部Mono的一些数据更新外部Mono的数据。)

 @GetMapping("/search")
    public Mono<Github> combineGithubData() {
        WebClient client = WebClient.create("https://api.github.com");
        Mono<Github> data = client.get().uri(URI.create("https://api.github.com/users/autocorrectoff")).retrieve().bodyToMono(Github.class);
        data = data.map(s -> {
            client.get().uri(URI.create("https://api.github.com/users/Kukilej")).retrieve().bodyToMono(Github.class).map(m -> {
                s.setXXX(m.getName());
                return m;
            });

            return s;
        });
        return data;
    }

The field XXX is always returned as null, although I have set it to a value from inner Mono.

(尽管我已将其设置为内部Mono的值,但XXX字段始终返回null。)

I'm pretty sure this would work in RxJs.

(我很确定这可以在RxJ中使用。)

How do I make this work with Project Reactor?

(如何使用Project Reactor进行这项工作?)

edit: the code of the Github class

(编辑:Github类的代码)

import lombok.*;

@Getter @Setter
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Github {
    private String login;
    private int id;
    private String node_id;
    private String avatar_url;
    private String gravatar_id;
    private String url;
    private String html_url;
    private String followers_url;
    private String following_url;
    private String gists_url;
    private String starred_url;
    private String subscriptions_url;
    private String organizations_url;
    private String repos_url;
    private String events_url;
    private String received_events_url;
    private String type;
    private boolean site_admin;
    private String name;
    private String company;
    private String blog;
    private String location;
    private String email;
    private String hireable;
    private String bio;
    private int public_repos;
    private int public_gists;
    private int followers;
    private int following;
    private String created_at;
    private String updated_at;
    private String XXX;

}
  ask by ape_face translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your inner stream is not getting subscribed to.

(您的内部流没有被订阅。)

Either us flatMap , or better yet, use zip :

(我们要么用flatMap ,要么更好用zip :)

data
    .zipWith(client.get().uri(...).retrieve().bodyToMono(Github.class))
    .map(tuple2 -> {
        //update tuple2.getT1() with m.getName() and return the updated tuple
        return tuple2.mapT1(tuple2.getT1().setXXX(tuple2.getT2().getName()));
    })
    .map(tuple2 -> tuple2.getT1() //updated s);

zipWith() subscribes to the inner stream.

(zipWith()订阅内部流。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...