简体   繁体   中英

Spring WebFlux WebClient: delay execution

When I define and call a client method like this:

webClient.get()
            .uri(endpoint)
            .headers(this::configureHttpHeaders)
            .retrieve()
            .bodyToMono(String.class)

the retrieve method gets executed even if I do nothing with the returned Mono.

I would have expected that it only gets called when someone (a subscriber ?) actually want to get data from the Mono.

Why does it get called immediately? How can I change this behaviour?

For more context: in my case I only want it to be executed when CacheFlux onCacheMissResume gets triggered:

    Flux<T> defaulValue= client.getStringMono()
            .flatMapIterable(mapper::readObjects);

    CacheFlux.<String, T>lookup(key -> readFluxFromCache(cacheName, key), cacheKey)
                .onCacheMissResume(defaulValue)
                .andWriteWith((key, valueSignal) -> writeFluxToCache(cacheName, key, valueSignal))

I think it has to do with what is called hot and cold publishers.

You can read more about them here projectreactor.io/docs/core/release/reference/#reactor.hotCold To transform a hot publisher to a cold publisher you can use the defer function.

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