how to run methods and check for final result in spring WebFlux without transforming

Viewed 72

I have a method called fluxFromFileStream that returns a Flux of String. after handling those string and doing some DTO operations, I have to save them into a mongodb database using methods transformAndSaveKpis(kpiHdfsDtoFlux) and transformAndSaveReports(kpiHdfsDtoFlux).

public void transformAndSaveKpisAndReports(InputStream inputStream, DQSCJobName jobName) {
    fluxFromFileStream(inputStream)
            .flatMap(this::buildDTOFromLine)
            .map(kpiHdfsDto -> changeKpiTypeToFreezeIfJobNameIsFrozen(jobName, kpiHdfsDto))
            .cache()
            .transform(
                    kpiHdfsDtoFlux -> Flux.zip(transformAndSaveKpis(kpiHdfsDtoFlux), transformAndSaveReports(kpiHdfsDtoFlux))
            );
}

The problem with that is the method .transform() is returning a value of flux that I will not need. what I need actually is to verify if the reactive stream has been achieved successfully without any problem, else throw an exception inside my main method (transformAndSaveKpisAndReports).

Before, I was checking the result of the whole stream (including .transform) if it's null then throw an exception, but appears to me that's not really the clean way to do things.

bellow are the methods I'm calling inside the transform method:

private Flux<Kpi> transformAndSaveKpis(Flux<KpiHdfsDto> kpiHdfsDtoFlux) {
    return kpiHdfsDtoFlux
            .map(this::kpiHdfsDtoToKpiDocument)
            .collectList()
            .flatMapMany(kpis -> kpiRepository.insertAll(kpis));
}

private Flux<Report> transformAndSaveReports(Flux<KpiHdfsDto> kpiHdfsDtoFlux) {
    return kpiHdfsDtoFlux
            .flatMap(this::kpiHdfsDtoToReportDocument)
            .groupBy(Report::getType)
            .flatMap(reportList -> reportRepository.insertAll(reportList.collectList()));

}
0 Answers
Related