如何在`.reduce(...)`函数中使用`Mono.subscriberContext()`

时间:2019-06-21 06:11:58

标签: java spring-boot spring-webflux project-reactor

我正在处理大量的对象,并且试图减少它,而在接收到的Monos上下文中保存的数据需要在reduce函数中使用。

//I've reduced it successfully But I don't have the Context Data :( 
return Flux.merge(someClient.getSomeDTO(request.getId()),
                  anotherClient.getAnotherDTO(request.getId())
                )
                .onErrorMap(e -> new ResponseStatusException(
                        HttpStatus.BAD_REQUEST,
                        "An error occurred!", e))
                .reduce(OutboundDTO.builder(),
                        (builder, dto) -> 
                                pageAdapter.buildOutboundDTO(builder, dto, null/*Empty*/)
                )
                .flatMap(builder -> ServerResponse.ok()
             .body(BodyInserters.fromObject(builder.build())));
/*This Doesn't Work It gave compilation error 
and it works well with .map(...) but this means I can't reduce it! */
return Flux.merge(someClient.getSomeDTO(request.getId()),
                  anotherClient.getAnotherDTO(request.getId())
                )
                .onErrorMap(e -> new ResponseStatusException(
                        HttpStatus.BAD_REQUEST,
                        "An error occurred!", e))
                .reduce(OutboundDTO.builder(),
                        (builder, dto) -> Mono.subscriberContext().flatMap(ctx->
                                pageAdapter.buildOutboundDTO(builder, dto, ctx.get(SavedObject.class))
                        )
                )
                .flatMap(builder -> ServerResponse.ok()
             .body(BodyInserters.fromObject(builder.build())));

我希望Mono.subscriberContext()的主要目的是在下面的所有链条中共享,不仅是像reference guide中提到的map这样的方法, 因此问题是,是否可以在.reduce函数中进行操作,或者是否有替代方法,或者如果不可能,那么为什么知道呢? :)

0 个答案:

没有答案