简体   繁体   中英

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.

You can use Mono.fromCallable + delayElement within a flatMap like this:

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. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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