简体   繁体   中英

Executing Mono streams that no one subscribes to in Spring Web flux

I have a spring Webflux application. There are two important parts to this application:

  1. A job is scheduled to run at a fixed interval.
  2. The job fetches the data from DB and stores the data in Redis.
void run() {
  redisAdapter.getTtl()
    .doOnError(RefreshExternalCache::logError)
    .switchIfEmpty(Mono.defer(() -> {
        log.debug(">> RefreshExternalCache > refreshExternalCacheIfNeeded => Remaining TTL could not be retrieved. Cache does not exist. " +
                "Trying to create the cache.");
        return Mono.just(Duration.ofSeconds(0));
    }))
    .subscribe(remainingTtl -> {
        log.debug(">> RefreshExternalCache > refreshExternalCacheIfNeeded => original ttl for the cache: {} | ttl for cache in seconds = {} | ttl for cache in minutes = {}",
                remainingTtl, remainingTtl.getSeconds(), remainingTtl.toMinutes());

        if (isExternalCacheRefreshNeeded(remainingTtl, offerServiceProperties.getExternalCacheExpiration(), offerServiceProperties.getExternalCacheRefreshPeriod())) {
            log.debug(">> RefreshExternalCache > refreshExternalCacheIfNeeded => external cache is up-to-date, skipping refresh");
        } else {
            log.debug(">> RefreshExternalCache > refreshExternalCacheIfNeeded => external cache is outdated, updating the external cache");
            offerService.refreshExternalCache();
        }
    });
}

This basically calls another method called refreshExternalCache() , the implementation below:

public void refreshExternalCache() {
    fetchOffersFromSource()
        .doOnNext(offerData -> {
            log.debug(LOG_REFRESH_CACHE + "Updating local offer cache with data from source");
            localCache.put(OFFER_DATA_KEY, offerData);
            storeOffersInExternalCache(offerData, offerServiceProperties.getExternalCacheExpiration());
        })
        .doOnSuccess(offerData -> meterRegistry.counter(METRIC_EXTERNAL_CACHE_REFRESH_COUNTER, TAG_OUTCOME, SUCCESS).increment())
        .doOnError(sourceThrowable -> {
            log.debug(LOG_REFRESH_CACHE + "Error while refreshing external cache {}", sourceThrowable.getMessage());
            meterRegistry.counter(METRIC_EXTERNAL_CACHE_REFRESH_COUNTER, TAG_OUTCOME, FAILURE).increment();
        }).subscribe();
}

Also, in the above method, you can see a call to storeOffersInExternalCache

public void storeOffersInExternalCache(OfferData offerData, Duration ttl) {
    log.info(LOG_STORING_OFFER_DATA + "Storing the offer data in external cache...");
    redisAdapter.storeOffers(offerData, ttl);
}
public void storeOffers(OfferData offerData, Duration ttl) {
    Mono.fromRunnable(() -> redisClient.storeSerializedOffers(serializeFromDomain(offerData), ttl)
        .doOnNext(status -> {
            if (Boolean.TRUE.equals(status)) {
                log.info(LOG_STORE_OFFERS + "Data stored in redis.");
                meterRegistry.counter(METRIC_REDIS_STORE_OFFERS, TAG_OUTCOME, SUCCESS).increment();
            } else {
                log.error(LOG_STORE_OFFERS + "Unable to store data in redis.");
                meterRegistry.counter(METRIC_REDIS_STORE_OFFERS, TAG_OUTCOME, FAILURE).increment();
            }
        }).retryWhen(Retry.backoff(redisRetryProperties.getMaxAttempts(), redisRetryProperties.getWaitDuration()).jitter(redisRetryProperties.getBackoffJitter()))
        .doOnError(throwable -> {
            meterRegistry.counter(METRIC_REDIS_STORE_OFFERS, TAG_OUTCOME, FAILURE).increment();
            log.error(LOG_STORE_OFFERS + "Unable to store data in redis. Error: [{}]", throwable.getMessage());
        })).subscribeOn(Schedulers.boundedElastic());
}

Redis Client

@Slf4j
@Component
public class RedisClient {
    private final ReactiveRedisTemplate<String, String> reactiveRedisTemplate;
    private final ReactiveValueOperations<String, String> reactiveValueOps;

    public RedisClient(@Qualifier("reactiveRedisTemplate") ReactiveRedisTemplate<String, String> reactiveRedisTemplate) {
        this.reactiveRedisTemplate = reactiveRedisTemplate;
        this.reactiveValueOps = reactiveRedisTemplate.opsForValue();
    }

    Mono<Optional<String>> fetchSerializedOffers() {
        return reactiveValueOps.get(OFFER_DATA_KEY).map(Optional::ofNullable);
    }

    Mono<Boolean> storeSerializedOffers(String serializedOffers, Duration ttl) {
        return reactiveValueOps.set(OFFER_DATA_KEY, serializedOffers, ttl);
    }

    Mono<Duration> getTtl() {
        return reactiveRedisTemplate.getExpire(OFFER_DATA_KEY);
    }
}

Now my concerns are:

  1. If I do not call the subscribe method on these Mono streams, these methods are not even executed. This is fair as they won't execute until someone subscribes to them.
  2. As I understand it correctly, subscribe is a blocking call. This defeats the whole purpose of Reactive programming. Isn't it?
  3. I looked for several ways to make this work, one of them has been shown above. I tried calling one of the methods in Mono.fromRunnable but this also is not a very good approach. (read it on another thread in StackOverflow).

So, is the approach that I am taking above not correct? How do we execute the Mono streams that no one subscribes to?

Answering your concern number 2 (which seems to be the only real doubt in your question). Not really. block() ( https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html#block-- ) is the one that subscribes to a Mono or Flux and waits indefinitely until a next signal is received. On the other hand subscribe() ( https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html#subscribe-- ) subscribes to a Mono or Flux but it doesn't block and instead reacts when an element is emitted.

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