Reactive Advice

从 5.3 版本开始,可以将 ReactiveRequestHandlerAdvice 用于产生 Mono 回复的请求消息处理程序。必须为此 advice 提供 BiFunction<Message<?>, Mono<?>, Publisher<?>>,并且它是从拦截的 handleRequestMessage() 方法实现产生的回复上的 Mono.transform() 运算符进行调用的。通常,当我们希望通过 timeout(), retry() 和类似的支持运算符来控制网络波动时,就需要进行这样的 Mono 定制。例如,当我们可以通过 WebFlux 客户端调用 HTTP 请求时,我们可以使用以下配置来不超过 5 秒等待响应:

Starting with version 5.3, a ReactiveRequestHandlerAdvice can be used for request message handlers producing a Mono replies. A BiFunction<Message<?>, Mono<?>, Publisher<?>> has to be provided for this advice and it is called from the Mono.transform() operator on a reply produced by the intercepted handleRequestMessage() method implementation. Typically, such a Mono customization is necessary when we would like to control network fluctuations via timeout(), retry() and similar support operators. For example when we can an HTTP request over WebFlux client, we could use below configuration to not wait for response more than 5 seconds:

.handle(WebFlux.outboundGateway("https://somehost/"),
                       e -> e.customizeMonoReply((message, mono) -> mono.timeout(Duration.ofSeconds(5))));

message 参数是消息处理程序的请求消息,可用于确定请求范围属性。mono 参数是此消息处理程序的 handleRequestMessage() 方法实现的结果。还可以从此函数调用嵌套的 Mono.transform(),以应用例如 Reactive Circuit Breaker

The message argument is the request message for the message handler and can be used to determine request-scope attributes. The mono argument is the result of this message handler’s handleRequestMessage() method implementation. A nested Mono.transform() can also be called from this function to apply, for example, a Reactive Circuit Breaker.