0
votes

Hi , I have Flux and during iteration over each element it's create new mono . I also have other mono outside of flux . And want to do the following : When flux (with corresponding insides mono's end ) then do second mono . The challegeing prart is that flux inside mono creates from webclient request . As starting point please look at "load" method . Basicly without webclient it works , but in case with webclient inside map work after then . Using spring-boot 2

public WebClient.ResponseSpec sendGetRequest(String path, Object... pathVariables){
   try {
       LOGGER.info("content type {}, url {}, path {}", contentType, url, path);
       WebClient.ResponseSpec responseSpec = sendRequest(HttpMethod.GET, contentType, authorizationToken, url, path, pathVariables);
       return responseSpec;
   }catch (Exception e){
       throw new WebClientProcessingException("Exception when trying to process", e);
   }
}

public Mono<PersonPayload> loadPerson(String  path){
    try {
        LOGGER.info("path {}", path);
        Mono<QuestionDetailsPayload> person = sendGetRequest(path).bodyToMono(PersonPayload.class);
        return person;
    }catch (Exception e){
        throw new WebClientProcessingException("Exception when trying to process",e);
    }
}


public Mono<PersonDomain> getPerson(String path) {
    Assert.notNull(path, "path can't be null");
    try{
        LOGGER.info("path {}" ,path);
        Mono<PersonPayload> personPayload = loadPerson(path);
        return personPayload.map(this::toPersonDomain);
    }catch (Exception e){
        throw new PersonNotFoundException("Exception when trying to get person info" , e);

    }
}

public PersponDomain toPersonDomain(PersonPayload personPayload){
    return modelMapper.map(personPayload, PersonDomain.class);
}

public void load(){
    List<String> outStr = Arrays.asList("out1", "out2","out3");
    Flux flux = Flux.fromIterable(outStr);
    Flux<Mono<PersonDomain>> results =  flux.map(string ->{
        System.out.println(string);
        Mono<PersonDomain> personMono = getPerson("inside");
        Mono<String> result = personMono.map(h ->{
            System.out.println(personMono.getName());
            return personMono.getName() + "_test";
        });
        return result;
    });
    Mono<String> second = Mono.just("second");
    results.then(second);
    results.subscribe(stringMono -> {
        stringMono.subscribe();
    });
    second.subscribe( s->{
        System.out.println(s);
    });
}

Gradle dependency :

implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springframework.boot:spring-boot-starter-validation'

implementation 'org.postgresql:postgresql'
implementation 'org.springframework.boot:spring-boot-starter-jooq'
implementation 'org.jooq:jooq-codegen'

implementation 'org.modelmapper:modelmapper:2.3.0'
implementation 'org.modelmapper:modelmapper:2.3.0'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

implementation 'com.google.code.gson:gson:2.8.5'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
testImplementation 'org.powermock:powermock-module-junit4:2.0.0'
testImplementation 'org.powermock:powermock-api-mockito2:2.0.0'}
2
what exactly you are trying to do? combining the response of Mono with every Flux element emitted? I can see accoding to your code snippet return type of first Flux should be Flux<Mono<String>> not Flux<Mono<PersonDomain>>. More over you should use flatMap when using Webclient.akreddy.21

2 Answers

0
votes

Flux#map is a synchronous operation and does not subscribe to the returned object.

You should use Flux#flatMap/Flux#concatMap/Flux#flatMapSequential/Flux#switchMap. These operators will subscribe to the returned Publisher.

0
votes

Solution for my case to use :

subscribe(Consumer<? super T> consumer,
          Consumer<? super Throwable> errorConsumer,
          Runnable completeConsumer); 

instead of then. Which deal with values and errors but also execute some code when the sequence successfully completes.