[英]Only one connection receive subscriber allowed
Can someone help me with Only one connection receive subscriber allowed.
有人可以帮助我吗?
Only one connection receive subscriber allowed.
error? 错误?
I looked at Oleh Dokuka's answer but it did not help me. 我看着Oleh Dokuka的回答,但没有帮助我。
I have simplified the code for demonstration purpose. 我已经简化了代码以进行演示。 In my actual code I am getting a bulk Json request, I need to query two different tables taking two different parameters from the request body, call another service using both the results and send the result in the response.
在我的实际代码中,我收到一个批量Json请求,我需要查询两个不同的表,这些表从请求主体中获取两个不同的参数,使用这两个结果调用另一个服务,并将结果发送到响应中。
Router function 路由器功能
@Bean
public RouterFunction<ServerResponse> myRoute(MyRequestHandler myRequestHandler) {
return route(RequestPredicates.POST("/api/something"), myRequestHandler::myHandlerFunction);
}
Handler function 处理函数
public Mono<ServerResponse> myHandlerFunction(ServerRequest serverRequest) {
Mono<Integer> just = Mono.just(22);
//For simplification I've added String body here. In actual code I have proper json body
Mono<String> stringMono = serverRequest.bodyToMono(String.class);
Mono<String> mono = stringMono.zipWith(stringMono).map(t -> t.getT2() + t.getT1());
return ok().body(mono, String.class);
}
The code is working fine if I replace stringMono
with just
in both the places in 如果我
just
在以下两个位置替换stringMono
,则代码工作正常
Mono<String> mono = stringMono.zipWith(stringMono).map(t -> t.getT2() + t.getT1());
Why is it working with Mono<String> mono = just.zipWith(just).map(t -> t.getT2() + t.getT1());
为什么与
Mono<String> mono = just.zipWith(just).map(t -> t.getT2() + t.getT1());
Thanks in advance. 提前致谢。
It looks like the stringMono.zipWith(stringMono)
will cause Spring to attempt to subscribe to the body of the request twice which is likely your problem since the ServerRequest is unicast and can have only one subscriber. 看起来
stringMono.zipWith(stringMono)
会使Spring尝试两次订阅请求的主体,这很可能是您的问题,因为ServerRequest是单播的,只能有一个订阅者。
Try this: 尝试这个:
Mono<String> stringMono = serverRequest.bodyToMono(String.class).publish(body -> body.zipWith(body).map(t -> t.getT2() + t.getT1()));
publish() will not cause multiple subscriptions to the body. publish()不会导致对正文的多个订阅。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.