简体   繁体   English

如何使用来自 Mono 的数据添加延迟?

[英]How do I add a delay with data from the Mono?

I have a service that is returning a value which contains delay information.我有一项服务正在返回一个包含延迟信息的值。

  public Mono<R> authenticate(
      A authenticationRequest,
      @RequestHeader Map<String, String> headers,
      ServerHttpResponse serverHttpResponse) {

    final AuthServiceResponse<R> authenticationResponse = authService.authenticate(authenticationRequest, headers);
    serverHttpResponse.setStatusCode(authenticationResponse.getStatusCode());
    return Mono.just(authenticationResponse.getOperationResponse())
        .delayElement(authenticationResponse.getDelay());
  }

I'd like to try to convert it so it is reactive I got this far...我想尝试转换它,所以它是反应性的,我已经走到这一步了......

  public Mono<R> authenticate(
      A authenticationRequest,
      @RequestHeader Map<String, String> headers,
      ServerHttpResponse serverHttpResponse) {

    return authService.authenticate(authenticationRequest, headers)
            .map(authenticationResponse->{
              serverHttpResponse.setStatusCode(authenticationResponse.getStatusCode());          
              return authenticationResponse.getOperationResponse()
            });
      ...

but I wasn't sure how to add the "delayElement" capability.但我不确定如何添加“delayElement”功能。

You can use Mono.fromCallable + delayElement within a flatMap like this:您可以在flatMap中使用Mono.fromCallable + delayElement ,如下所示:

return authService.authenticate(authenticationRequest, headers)
         .flatMap(authenticationResponse -> {
           return Mono.fromCallable(() -> authenticationResponse.getOperationResponse())
                      .delayElement(authenticationResponse.getDelay())
            });

One thing to note... you cannot pass ServerHttpResponse in this situation as a parameter, but you have ServerWebExchange which has the request and response along with the headers.需要注意的一件事......在这种情况下您不能将ServerHttpResponse作为参数传递,但是您有ServerWebExchange ,其中包含请求和响应以及标头。 The complete solution is完整的解决方案是

public Mono<R> authenticate(
      @RequestBody SimpleAuthenticationRequest authenticationRequest,
      ServerWebExchange serverWebExchange) {

    return authService
        .authenticate(authenticationRequest, serverWebExchange.getRequest().getHeaders())
        .doOnNext(
            serviceResponse ->
                serverWebExchange.getResponse().setStatusCode(serviceResponse.getStatusCode()))
        .flatMap(
            serviceResponse ->
                Mono.fromCallable(serviceResponse::getOperationResponse)
                    .delayElement(serviceResponse.getDelay()));
}

Try this to add delay based on your authenticationResponse.getDelay() value尝试根据您的 authenticationResponse.getDelay() 值添加延迟

public Mono<Object> authenticate(Object authenticationRequest,@RequestHeader Object headers,
        Object serverHttpResponse) {

    return authenticate(authenticationRequest,headers)
            .flatMap(authenticationResponse -> {
                Mono<String> delayElement = Mono.just("add delay")
                        .delayElement(Duration.ofSeconds(authenticationResponse.getDelay()));
                Mono<Object> actualResponse =Mono.just(authenticationResponse.getOperationResponse());
                return Mono.zip(delayElement,actualResponse).map(tupleT2 -> tupleT2.getT2());
            });

}

let me know if it doesn't work.如果它不起作用,请告诉我。 i will try to find other way.我会尝试寻找其他方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM