如何在Flux(Parent)中查找所有数据是由其内部非阻塞Flux或Mono(child)处理的?

时间:2018-08-16 03:27:38

标签: reactive-programming spring-webflux project-reactor

我有一个聚合器实用程序类,在这里我必须合并多个cassandra表数据。我的生产代码如下所示,但不完全相同。

@Autowired FollowersRepository followersRepository;
    @Autowired TopicRepository topicRepository;
        @GetMapping("/info")
        public Flux<FullDetails> getData(){
            return Flux.create(emitter ->{
                followersRepository.findAll() 
                .doOnNext(data -> {
                    List<String> all = data.getTopiclist(); //will get list of topic id
                    List<Alltopics> processedList = new ArrayList<Alltopics>();
                    all.forEach(action -> {
                        topicRepository.findById(action) //will get full detail about topic
                        .doOnSuccess(topic ->{
                            processedList.add(topic);
                            if (processedList.size() >= all.size()) {
                                FullDetails fulldetails = new FullDetails(action,processedList);
                                emitter.next(fulldetails);
                                //emitter.complete();
                            }
                        })
                        .subscribe();
                    });
                })
                .doOnComplete(() ->{
                    System.out.println("All the data are processed !!!");
                    //emitter.complete(); // executing if all the data are pushed from database not waiting for doOnNext method to complete.
                })
                .subscribe();
            });
        }

有关更多详细信息,请在此处CodeLink中引用代码。

我尝试使用doOnComplete和doOnFinally处理外部Flux,它没有等待所有内部非阻塞调用完成。

在处理Flux内部所有嵌套的Mono / Flux(非阻塞)请求之后,我想调用onComplete。

对于嵌套阻塞通量/单声道,内部通量/单声道完成后将执行外部通量doOnComplete方法。


PostScript(PS):-

在下面的示例中,我找不到在哪里放置generator.complete()。 因为doOnComplete()方法是在所有内部Mono完成之前调用的。

请求正文:-

[{ "content":"Intro to React and operators", "author":"Josh Long", "name":"Spring WebFlux" },{ "content":"Intro to Flux", "author":"Josh Long", "name":"Spring WebFlux" },{ "content":"Intro to Mono", "author":"Josh Long", "name":"Spring WebFlux" }] 

我的休息控制器:-

@PostMapping("/topics")
    public Flux<?> loadTopic(@RequestBody Flux<Alltopics> data)
    {
        return Flux.create(emitter ->{
            data
            .map(topic -> {
                topic.setTopicid(null ==topic.getTopicid() || topic.getTopicid().isEmpty()?UUID.randomUUID().toString():topic.getTopicid());
                return topic;
            })
            .doOnNext(topic -> {
                topicRepository.save(topic).doOnSuccess(persistedTopic ->{
                    emitter.next(persistedTopic);
                    //emitter.complete();
                }).subscribe();
            })
            .doOnComplete(() -> {
                //emitter.complete();
                System.out.println(" all the data are processed!!!");
            }).subscribe();
        });
    }

0 个答案:

没有答案