简体   繁体   中英

Nested Webclient calls is giving an error

Scenario: need to get an access token from a service and pass it to a webclient call as below.

     return someservice
            .getToken()  //returns token as Mono<String>,this itself is another webclient call
            .flatMap(token -> {
                return customWebclient.delete() //observe the delete method here
                        .uri(uri -> uri.path(/users)
                        .queryParam("id", id)
                        .build())
                        .headers(headers -> headers.setBearerAuth(token))
                        .header("Content-Type", MediaType.APPLICATION_JSON_VALUE)
                        .header("Accept", MediaType.APPLICATION_JSON_VALUE)
                        .retrieve()
                        .bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {
                        }).log();
            });  // this return a Mono<Map<String, Object>>

I then need to block this final result and so I am using.toFuture().get() to get Map<String, Object>.

Now the issue is.get() call here is waiting indefinitely and the call to customWebClient call is never happening and if I get use.get(3000, TimeUnit.SECONDS), get() is throwing a TimedOutException and then calling the customWebClient call.

From what I understand, get() method should wait for Mono<Map<String, Object>> to resolve ie, customWebclient call to happen and then return the result.

Using spring-boot-starter-webflux

Please help me with a solution.

I have also tried not nesting these calls and used toFuture().get() for both token and the Map, get() for token is waiting forever.

Other important point is that the same customWebclient call for get() method in same way as example is working fine.

Try with adding ".subscribeOn(Schedulers.boundedElastic())" before.toFuture().get(30L, TimeUnit.SECONDS).

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